- Home /
Renaming a GameObject Clone
spawnObject is just the object to be cloned dragged into a public variable. spawner is a empty gameobejct for where they spawn.
Heres the code, inside a incrementing while loop:
GameObject clone = Instantiate (spawnObject.transform, spawner.transform.position, spawner.transform.rotation) as GameObject;
clone.name = "spagetti" + Protifuge_Count;
I keep getting this error during run time:
NullReferenceException: Object reference not set to an instance of an object Debug_Camera+c__Iterator0.MoveNext () (at Assets/IOM/Scripts/Debug_Camera.cs:40)
I've tried several different things and don't know what the problem is...
Answer by whydoidoit · Mar 23, 2014 at 12:22 PM
You are doing Instantiate on a transform and then trying to cast the result to be a GameObject. Instantiate returns the type you pass in, and Transform is not related to GameObject so the result of your cast is null. Cast the return value to Transform and make clone a Transform and it will work as it is.
Thanks, I see where I went wrong here. Thanks for the help!
$$anonymous$$
$$anonymous$$agnet_man16 could you please provide the script example how you solved it? I am facing the same problem but unable to fix it based on the answer given.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
unknown identifer explosion problem 2 Answers
Having problems when using triggers 1 Answer
Assets/MELLE.js(1,15): BCE0044: unexpected char: ';'. 1 Answer
Prefab is not working anymore after opening my project. 3 Answers