Object reference to set to instance on an instantiated object

I have a C# script where I am instantiating a prefab inside of a coroutine. When I attempt to access that prefab from another coroutine or anywhere else in the code, I am getting a null reference exception; however, I know that the object exists, as I can see it on-screen, and I can reference it later in the same coroutine that instantiates it. Here is the relevant code:

If I call StartCoroutine (SpawnPrefab ()), the prefab is instantiated. I can see it on the screen and it behaves as expected. The print statement accesses the instantiated prefab's position and prints it correctly. However, if I attempt to acces the instantiated prefab anywhere else -- in Update, or another coroutine, for example -- I get the null reference exception. So the following will throw that error:

1 Reply

I tried to repo your problem and failed, so something more is going on. It occurred to me that you would have this problem if this script was attached to multiple objects, and you had masterPrefab initialized in only one of them. Here is the script I used to attempt to repro the problem: