Odd NullRerenceExcepetion


I was wondering if someone can tell me why I am getting this error. I am trying to find the child object called ‘sectionend’ of a prefab in the scene, and them Instantiate section2_prefab from this point. The object ‘section1_prefab1’ is definitely in the scene with its child object ‘sectionend’;

using UnityEngine;
using System.Collections;

public class TrackAssemb : MonoBehaviour {

	// Use this for initialization
	void Awake() 

        section1 = Resources.Load("Level_prototype_V1/section1_prefab", typeof(Transform)) as Transform;
        section2 = Resources.Load("Level_prototype_V1/section2_prefab", typeof(Transform)) as Transform;
        section3 = Resources.Load("Level_prototype_V1/section3_prefab", typeof(Transform)) as Transform;
        section4 = Resources.Load("Level_prototype_V1/section4_prefab", typeof(Transform)) as Transform;
        section5 = Resources.Load("Level_prototype_V1/section5_prefab", typeof(Transform)) as Transform;

    GameObject endPoint;
    bool flag = false;

	// Update is called once per frame
	void Update () 
        if(flag == false)
        // Find the position of the end point of the current section
         endPoint = GameObject.Find("section1_prefab1/sectionend");

        //gameObject.transform.position = endPoint.transform.position;

        //---Section 2

        // Create an instance of a section joining onto the end point found. 
        Transform Instance = Instantiate(section2, endPoint.transform.position, endPoint.transform.rotation) as Transform;

        flag = true;

Resources.Load() does not instantiate the object in the scene, but rather load the asset into memory. Furthermore, it only works with assets that are under a ‘Resources’ folder hierarchy.

You need to use Object.Instantiate() instead.

You cannot instantiate a child of a prefab. You can only instantiate the whole prefab. Nested prefabs are not supported yet, but UT is working on it.

Great information - thanks.

Just to clarify, the child object was only used to find the position of the end of the section1_prefab, the sections being instantiated are all parent objects.

Can I ask what the distinction is when using GameObject.Instantiate(), Object.Instantiate(), or Instantiate() by itself, etc?