Generic FindObjectsOfType in C# broken?

In the docs…

… it says this should work:

HingeJoint[] hinges = FindObjectsOfType<HingeJoint>();

But VisualC# says:

This seems to work, but talk about layers of slowness…

        Object[] hingeObjects = Object.FindObjectsOfType(typeof(HingeJoint));

        List<HingeJoint> hinges = new List<HingeJoint>();
        foreach (Object item in hingeObjects)
        {
            hinges.Add((HingeJoint)item);
        }

Is the generic version from the docs a myth?

Yes.

It does not appear to exist, no matter what the docs say.

Same story with GetComponent();

Um, but GetComponent() does exist.

Care to clarify?

@rsx

I was referring to the post above me:

FindObjectsOfType has no generic version, but GetComponent does as I use that all the time.

I remember saw already such thread about that generic issue , well don’t remember the link but I kept the piece of code that allow you to implement it yourself

public static T[] FindObjectsOfType<T>()
{
   T[] objects = UnityEngine.Object.FindObjectsOfType(typeof(T)) as T[];
   return objects;
}

I’ve been told casting with round brackets is faster than the ‘as’ keyword. Either way is a LOT slower than a generic though.

I wish someone from Unity would reply with a big tracking reference or some comment.

Thanks for the replies from the community though guys.

Cheers,

@cannon - after you replied, I tried two different projects, and confirmed it’s a .NET 2.1 generics discrepancy between Unity and Unity iPhone. Hopefully that will go away with the unified 3.0 editor. I knew I wasn’t that crazy :slight_smile: just spend too much time in the iPhone editor!