- Home /
Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object
Hi I am trying to instantiate a prefab and add the prefab to a gameobject that already exists in the world. I have researched the error and it seems that I need to declare the gameobject I want to use as the parent in the start method but I am confused. Here is my code...
void OnTriggerEnter(Collider collider)
{
GameObject stageSegment = Instantiate(A, new Vector3 (0,0,0), Quaternion.identity) as GameObject;
stageSegment.transform.parent = GameObject.Find("Stage Scroll").transform;
Debug.Log(GameObject.Find("Stage Scroll"));
}
And this is the error I get from the Debug.log output...
NullReferenceException: Object reference not set to an instance of an object LevelLoadingTrigger.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/LevelLoadingTrigger.cs:78)
The gameobject I want to use as the parent already has a transform component so do I still need to declare it in Start and if so how do I go about declaring the gameobject I want to make the parent. The object instantiates just fine but it makes a clone of it in the hierarchy instead of becoming a child of my Stage Scroll game object - which only has a script attached to it and a transform component.
I am still new to learning C# and any help is much appreciated.
Your code looks correct to me. The NullReferenceException from your debug line is telling you that Unity is not able to find an object named "Stage Scroll". Perhaps you spelt it without a space, using different casing, or maybe its not active?
I tried that already dude - with/without spaces, camel casing. I've tried all sorts. When you say if its active or not, what do you mean?
The stage scroll object in question is an object in my hierarchy. Like I have said above its just got a transform component on it a script which moves the object across the screen at a s$$anonymous$$dy pace. Its an empty gameobject in the sense that it has no 3D objects attached to it, just a transform component to allow movement.
thumbs up for the excellent diagram!
active objects have a tick to the left of their name in the inspector. inactive objects don't have a tick (obviously!) and also appear dimmed in the hierarchy. looking at your diagram I don't think that's the problem. hmm, i'm stumped.
Sry didn't see new posts, whas to fast...
You are declaring A as a transform in the header.
But you are casting it to a GameObject, that results in a null.
Ins$$anonymous$$d you should change the declaration to "public GameObject A"
Edit:
Argh, again to late... :D
Answer by Kebabs · Dec 06, 2013 at 10:10 PM
OK guys it is now working :)
THANK YOU BOTH @chillersanium and @karlhulme
I just had to ctrl+z back to my default code from the start of the question, make A a public gameobject and now when the game is running it becomes a child of stage scroll!!!!!
Thank you both for the help!! I'm so new to coding and help like this inspires me to keep learning and understanding why code works/does not work. THANK YOU SOO MUCH!! This is for my studies so seriously again THANK YOU BOTH :)
void OnTriggerEnter(Collider collider)
{
GameObject stageSegment = Instantiate(A, new Vector3 (0,0,0), Quaternion.identity) as GameObject;
stageSegment.transform.parent = GameObject.Find("Stage Scroll").transform;
Debug.Log(GameObject.Find("Stage Scroll"));
}
Looks like somebody got happy :D
Was happy to help, for that is the Answers section.
Tipps for further questions:
Do give the full code
Also point out on which line the error is pointing (if you don't give the whole header to)
Perhaps try do debug first, in $$anonymous$$onoDevelop that is a real easy task
Greetings Chillersanim