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?
– karlhulmeI 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?
– KebabsThe 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 steady pace. Its an empty gameobject in the sense that it has no 3D objects attached to it, just a transform component to allow movement. [18993-untitled-1.jpg|18993]
– Kebabsthumbs 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.
– karlhulme:) As am I. I have just tried adding it to other objects in my hierarchy and no luck. I get the same error and my instantiated game object makes its own group in the hierarchy.
– Kebabs