Beginner topic: Trying to Understand a script error ~NULL REFERENCE EXCEPTION(SOLVED)

THE CODE

using UnityEngine;

[System.Serializable]
public class ColorToPrefab {

    public Color32 color;
    public GameObject prefab;
    public int index;
    public string name;
}
using UnityEngine;

public class LevelGenerator : MonoBehaviour {

    public Texture2D map;

    public ColorToPrefab[] colorMappings;

    GameObject[,]prefabTileMap;

    void Start () {
        GenerateLevel();
    }

    void GenerateLevel ()
    {
        Color32[] pColor = map.GetPixels32 ();
        int w = map.width;
        int h = map.height;
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                Debug.Log ("* * * * *   GeneratingTile --> x: " + x + ", y: " + y+"    * * * * *");
                int index = x + y * w;
                GenerateTile(pColor[index],x, y);
            }
        }
    }

    void GenerateTile (Color32 color, int x, int y)
    {
        Debug.Log ("c: "+color);
        if (color.a == 0) {
            // The pixel is a path. Let's ignore it!
            Debug.Log("PATH FOUND -RETURN-");
            prefabTileMap [x, y] = null;
            return;
        }

        foreach (ColorToPrefab colorMapping in colorMappings)
        {
            Debug.Log ("ColorMapping.color: "+colorMapping.prefab.name);
            if (colorMapping.color.Equals(color))
            {
                Debug.Log ("ColorMapping.color == pixelColor --> CREATING OBJECT");
                Vector3 position = new Vector3(x, 1, y);
                GameObject go = (GameObject)Instantiate (colorMapping.prefab, position, Quaternion.identity, transform);

                prefabTileMap [x, y] = go; // THIS IS WHERE ERROR OCCURS
                GridIndex gi = go.GetComponent<GridIndex> ();
                gi.indexX = x;
                gi.indexY = y;
                return;
            }
        }
    }
 
}

THE ERROR

DESCRIPTION:
I’m trying to understand why I got the error. The list ColorToPrefab has a color, and a prefab tied to it as you can see in the code above.

Info on reference vs value types

In C# you have two data types, a reference type and a value type.

Classes (like GameObject) are reference types, structs (Vector3, Color) and primitives (int, double, float) are value types.

When you pass a value type into a method, you give it the value, not the object itself. The value of an int will be copied and given to the method.

With reference types, a variable storing one points to the object in memory (usually on the heap). When you give it to a method you give the method a pointer to the object, so both the caller and the callee are using the exact same object.

This also means a reference type can be null, ie pointing to nothing. A NRE is when you try to use a value from an object that doesn’t exist.

First check what value is null so you can find out why it is. Just put:

print(prefabTileMap ==null ? "tilemap is null" : "tilemap is instantiated");
print(go == null ? "go is null" : "go is instantiated");

I’m betting on it being the tilemap not being created, I can’t see an instantiation anywhere in your script.

1 Like

GameObject[,] prefabTileMap; on line 9 is where I declare it. I assumed that the array would be initialized with “null” pointers by default. But I’m not trying to GET the value inside an array, I’m trying to SET a value in the array. I would understand the error if I were referencing a value from the array, but I’m trying to set a value to the array.

Okay, I see why I got the error. The prefabTileMap variable was “NULL” value. I assumed the reference variable would have been initialized and then filled with “NULL” values for it’s cells. But just declaring the prefabTileMap only saved space I guess and did not assign it anything.

So, I added in prefabTileMap=new GameObject[w,h]; inside the GenerateLevel script and the code works fine. Crazy how things have to be put together.

You ARE referencing a value from the array. Arrays are reference types, and like I said:

In the case of a null array, you’re accessing the nth element of… what? The array doesn’t exist yet! Bork, throw a NRE.

Good to see you got it sorted anyway, there is a difference between declaration and assignment ;p