Adding "as GameObject" after instantiating

GameObject laser = Instantiate(
            projectile,
            transform.position,
            Quaternion.identity
        ) as GameObject

I’ve been following some tutorials and I’m wondering why sometimes I see “as GameObject” at the end of instantiating an object where you have already specified the variable type. Seems redundant. What are the use cases of doing this and what could happen if you don’t add “as GameObject”?

The flavor of Instantiate you are using returns a UnityEngine.Object.

Doing an “as GameObject” is a cast, which takes whatever you put on the left of the “as” and attempts to consider it what you put on the right of the “as”. If it can, it gives you back one of those things. If it can’t, it quietly returns null.

If you just want to Instantiate something, there is a generic method that you can use like so:

GameObject laser = Instantiate<GameObject>( projectile);

You don’t need the “as GameObject” because you’ve said to Instantiate, “I’m giving you a GameObject, you’re gonna make me a copy and return it as a GameObject.”

With that version you can make copies of just about anything in Unity, anything that derives from UnityEngine.Object I believe. The downside to it is that it doesn’t accept position and rotation as extra arguments, but you just add extra statements afterwards:

laser.transform.position = (whatever position);
laser.transform.rotation = (whatever rotation);
1 Like

I asked someone else and they mentioned that newer versions of unity implicitly convert the object into whatever you are trying to initialize it as without adding “as type” at the end. The people who are still using “as type” are still doing so just because it’s a habit.

Or because it’s in a tute… I remember when I first started in on Unity it was always a struggle to find a tutorial that was still-current, up-to-date, relevant. I wonder if it’s any better if you were starting out today?

Anyway, thanks for letting me know about language improvements… I didn’t know about the typing happening automatically… good to know though. Change, change, change, here we go!

1 Like

Almost correct. You may type it implicitly, i.e. omit the generic type param, but it doesn’t convert it implictly, simply because its still the generic version which will be used.
The compiler can use type inference to figure out which type of variable is passed in. That is, it still uses the generic and type safe version, but allows shorter syntax.

So why all the casting? Well, the casts had to be used in older versions, because many, many years ago, Unity did not have generic overloads of that method.
The only thing it accepted was the base type UnityEngine.Object. Thus no matter what you wanted to instantiate, it was taken as UnityEngine.Object and the copy was returned as such, so you’d need to upcast the result again.

If you happen to pass a template that is stored in a less specific type variable, you may still need a cast, because the compiler can only infer the type of the variable that you pass in.

3 Likes