- Home /
Instantiate example gives UnassignedReferenceException error
Blockquote> var cube : Transform; function Start () { for (var y = 0; y < 5; y++) { for (var x = 0; x < 5; x++) { var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity); } } }
This example code from the Unity User Manual gives me an UnassignedReferenceException error. I have followed the directions in the tutorial exactly. I created a GameObject cube, then created an empty prefab, then I dragged the cube object to the empty prefab, renamed the prefab and dropped it onto the default reference for the var cube in the inspector for this script. But when I run the code, the system tells me that cube has not been assigned.
SOLUTION: You must click on the GameObject that holds the script in the hierarchy when you make the assignment, via drag and drop. If you have just clicked on the script itself (under Projects), it is POSSIBLE to do the assignment that way, but it doesn't work when your game runs.
Never $$anonymous$$d! I went back to it and now it's working. Interesting that the physics acts a little differently with the prefab than it did with the "instantiate objects from scratch" example that preceded this one.
The procedure is to write the answer and mark it accepted, rather than writing "solved" in the title.