I have an object Candy, that gets instantiated from another GameObject. When Candy gets destroyed, I want it to spawn another GameObject called splat before Candy is destroyed.
So in the top I have:
var splat_obj : GameObject;
I have assigned a prefab called splat (which is a particle system) to the row splat_obj in the inspector, for Candy.js.
Then I instantiate it in Candy.js before Candy is destroyed
Try to select the message while the scene is playing. This will select the game object in which you find the component which throws this error. It might be that you accidentally added the script to another game object.
It selected Candy(clone), the issue was I did the assignment in the inspector to the script (Candy.js), but I should have assigned it to the prefab (CandyPrefab). Now it works!
How come I can assign things to the script? It’s kind of confusing.
I believe that it sets the script’s defaults to the ones assigned to the script when you attach it to an object. So, the order in which you do things matters. Pretty good odds this is the order you did these:
A) Create script
B) attach to object
C) write “var splat_obj” in the script & compile
D) drag the prefab into the slot in the script.
…but because the script was already attached, then when you did C, Unity assigned splat_obj its default value of null. In other words, if you attach the script to the object after you assign splat_obj in the script, it should contain the correct default value. Alternately, if you right-click “Reset” on the script, it should also set all the default values.