This question was
closed May 31, 2016 at 11:11 PM by
Le-Pampelmuse for the following reason:
Problem is not reproducible or outdated
Question by
karthickoxygen · May 31, 2016 at 11:10 PM ·
gameobjectinstantiatetransformnullreferenceexception
NullException Error while instantiating gameobjects
Hi,
There were two identical lines of instantiation code in my script. But the later one kept on giving NullException error in run time when being accessed (Line 4).
I was stumbled on this issue for an evening before I found out the answer.
Answer is in the first comment.
current_go_1 = Instantiate (first_go, position, Quaternion.identity) as GameObject;
current_go_2 = Instantiate (clicked, position, Quaternion.identity) as GameObject;
current_go_1.name = "1";
current_go_2.name = "2";
Comment
The "clicked" being instantiated in line 2 is actually a tranfsorm and the "first_go" is a gameobject.
When a transform is being instantiated as a GamObject, Unity is not giving any warning or error but simply gives a NullException error when we access that GameObject anywhere else in the script.
current_go_2 = Instantiate (clicked.gameobject, position, Quaternion.identity) as GameObject;
solved the problem.
Thanks.