- Home /
Object.Instantiate. , really?
Hey!
The Unity script reference mentions what looks like a C# generics version of Object.Instantiate(), Object.Instantiate.<> . Is this a typo? Is this a C# syntax I don't know about? In any case, I can't get a generic version to work in my C# script. Is the only option to cast the result into the desired type?
Many thanks!
Argh, threee excellent answers! Which one should I pick, which one should I pick?? Guess I'll go with Jessy's just to even reputations out a bit. :P
Answer by Jessy · Apr 24, 2011 at 05:27 AM
It does appear to be broken. It should look like this:
Instantiate<GameObject>(prefab);
But the closest you could get right now would be one of these
this.Instantiate<GameObject>(prefab);
prefab.Instantiate();
You would need a class with extension methods, for that:
using UnityEngine;
public static class ExtensionMethods {
public static T Instantiate <T> (this Object unityObject, T t) where T : Object { return Object.Instantiate(t) as T; }
public static T Instantiate<T>(this T unityObject) where T:Object { return Object.Instantiate(unityObject) as T; }
}
This is nowhere near as handy as the generic GetComponent, and seems pointless to me, especially given that it will carry overhead. I am interested to know if a generic Instantiate can be faster than using as, if Unity Technologies implements it.
The generic GetComponent also has overhead and looks similar to your implemenetation. In fact, they are calling GetComponent(typeof) as T; (or I can't remember if they used the cast operator. The strange bottom line is that I managed to do an exact similar GetComponent method with less overhead than the built in one. I was quite surprised! However, the price we pay is rather small. A typeof, and a cast.
How do you know what it looks like? Reflection? Here's some further info on that in case anyone finds Statement's post informative: http://forum.unity3d.com/threads/85229-can-you-use-AddComponent-with-a-custom-script?p=548746&viewfull=1#post548746
oh. three of the four great $$anonymous$$ds of Answers have found this, yet it's not fixed/corrected.
Yes Jessy, I have replied to another question with more details about it, but I couldn't find it. I'd link it if I had. I tend to mess around with Reflector from time to time to build a better understanding of the engine.
Right. What bothers me is that explicit cast and I thought I could get around it somehow. Oh well, not that big of a deal performance-wise anyway. If the return value ends up being cast anyway, I'd rather have it in my face than hidden behind a sexy function. Thanks everyone!
Answer by Eric5h5 · Apr 24, 2011 at 03:59 AM
It's JS syntax, like the generic version of GetComponent. I could never get it to work in either JS or C# though, so I have to wonder if it's actually implemented.
Answer by Statement · Apr 24, 2011 at 07:55 AM
The UnityEngine.dll doesn't even have a generic method for that. I checked it out with Reflector just now (Unity version 3.3.0f4).
- My guess is that it never made it into the final build somehow.
Even the docs for it seem a bit forgotton about. There isn't even a description for it. I'd disregard it.
Yeah that whole part of the docs seemed fishy to me, hence my question. Thanks!
Answer by Jeff-Kesselman · Sep 24, 2020 at 06:56 PM
AIR Once upon a time there was a T Instantiate<T>()
where T was a MonoBehavior. It would create a new game object, create the passed type of MonoBehavior ON that game object, and return a ref to the new MonoBehavior.
My best guess is that they eliminated that function but missed the reference to it in the docs.