Calling Instantiate without storing the returned object

I have seen several examples where a GameObject such as a player is spawned, and the return value from Instantiate is ignored. The game seems to play fine and I don’t see side effects, but why? Should’t the object return need to be stored?

public class GameManager : MonoBehaviour {
public GameObject player;
// Use this for initialization
void Start () {
SpawnPlayer();
}
private void SpawnPlayer(){
Instantiate(player,Vector3.zero,Quaternion.identity);
}
}

No, it doesn’t need. Unless your going to use that instantiated object in some way in the script.

It does not need to be stored as a GameObject variable in your script unless you intend on using it later. The Instantiate call actually tells Unity to create and keep track of an instance of the game object, which it will do within the current scene. If you were to assign the result of the Instantiate, it would just keep a reference to that Instance which actually lives within the Scene.

Hope this makes sense, please let me know if you need clarification at all. :slight_smile: