Hi everyone,
I’m doing a Unity 2D course and I was asked to instantiate an object (a Defender for a Plants vs Zombies game), and my solution was:
Instantiate (defender, transform.position, Quaternion.identity);
what my instructor used instead was:
GameObject newDefender = Instantiate(defender, transform.position, Quaternion.identity) as GameObject;
what is exactly the difference between the two codes?
Your first code does just instantiates the object.
The second code peace does in addition save the instantiated gameobject in a variable called newDefender.
1 Like
I see! it actually makes very much sense! Thank you!
And presumably, your instructor should be making use of the newDefender somewhere later in that block of code, otherwise there’s no reason to have stored the reference to it. Usually the compiler will give you a warning if you store a value in a variable but you never make use of that value. Perhaps a bit later in your instructor’s code they’re doing something with newDefender?