Help Need - The Thing you want to initiate is Null

Dear Community,

I receive the “The Thing you want to initiate is Null” Error. I have reviewed several threats but have been stuck for too long and am now looking for help.

What I essentially want to achieve: A box collider runs ahead of my character and when the box collider hits another box collider in a certain position I want to spawn trees from an array of 7 different prefab trees at the position of the collider

Thanks in advance for any help!

using UnityEngine;
using System.Collections;

public class MG1TreeLooper : MonoBehaviour {
   
    public GameObject[] treePrefabs;
    public Transform treePosition;
   
    void Start (){
        treePrefabs = new GameObject[7];
    }
   
    void OnTriggerEnter2D(Collider2D collider) {
        // Vector3 treePosition = new Vector3(treeLooper.GetComponent<Transform>(),0,0);
        Instantiate (treePrefabs[Random.Range (0,6)], treePosition.position, treePosition.rotation);
    }
}

Where are you assigning a value to treePrefabs, and treePosition?

With respect to the treePrefabs, I am attaching the 7 prefabs manually via the “public” function to the MG1TreeLooper GameObject which carries the script.

For the treePosition, I similarly reattach the MG1TreeLooper GameObject to itself via “public Transform” as this is where I want the trees to spawn.

Are you assigning your tree prefabs via inspector? If yes, you are essentially overwriting your populated array from inspector with the empty one in your Start() function:

void Start ()
{
        treePrefabs = new GameObject[7];
}

Also, irrelevant to the question, it is a bad practice to hard code random like this:

treePrefabs[Random.Range (0,6)]

In case you add 100 more prefabs, you will only pick those between 0 and 6. Also, if you reduce your prefab count to 3, you will get exceptions like array out of bounds.

You might want to write random picking like this:

void OnTriggerEnter2D(Collider2D collider)
{
     int randomIndex = Random.Range (0, treePrefabs.Length - 1);
     GameObject prefab = treePrefabs[randomIndex];
     Instantiate (prefab, treePosition.position, treePosition.rotation);
}

Also a bit nitpicking, but naming your variable:

public Transform treePosition;

will probably confuse someone thinking this variable is Vector3, since transform.position returns Vector3. Since you are using it essentially like a spawn point based on your previous snippet, you could rename it into something like:

public Transform spawnPoint;
public Transform target;
public Transform spawnerTransform;

Hope it helps!

Thanks a lot for this! I really appreciate the detail and nitpicking to learn about best practice. The code below is now working and the items are instantiated. It seems like I don’t need the Start() function at all. The prefabs don’t exactly show up where I want them to be and are always moved along to the right (along the x-axis) so need to figure out what is going on there.

    public GameObject[] treePrefabs;
    public Transform spawnPoint;

    void OnTriggerEnter2D(Collider2D collider)
    {
        int randomIndex = Random.Range (0, treePrefabs.Length - 1);
        GameObject prefab = treePrefabs[randomIndex];
        Instantiate (prefab, spawnPoint.position, spawnPoint.rotation);
    }
}