[SOLVED]NullReferenceExeption

I want to render blocks from a generated array with Vector3 positional information.
I am getting this error:

As I understand the problem lies in the array parameter that points to nothing? Still, can’t understand how to fix it. :rage:

    Vector3[] blockBuffer; //array to store generated information
    public Material material;
    public Mesh mesh;
    GameObject block_name;

    // Use this for initialization
    void Start () {
        blockBuffer = generateBlocks (); //generate block info and store it in the array variable
    }
   
    // Update is called once per frame
    void Update () {
        renderBlocks (blockBuffer); //render blocks with given info in form of array
    }

    public void renderBlocks(Vector3[] array){ //function to render the blocks
        foreach (Vector3 block in array) { //go through the given array
            GameObject block_name; //create a game object variable
             block_name = new GameObject ("block_name"); //create game object with name
            block_name.AddComponent<MeshFilter> ().mesh = mesh; //assign mesh
            block_name.AddComponent<MeshRenderer> ().material = material; //assign material
            block_name.AddComponent<Transform> ().transform.position = block; //assign new position from array at each iteration
        } 
    }
       
    Vector3[] generateBlocks(){ //generate blocks and store it in array
        Vector3[] blockArray = null; //initilize with null (don't know why)
        for (int i = 1; i > 9; i++) {
            blockArray[i] = new Vector3(-9f + 1f, 1f, 0f); //create a new vector3 and subtracting 1 from x pos each iteration to space out the blocks
        }
        return blockArray; //return the info
    }

Well, right now blockBuffer has no value. Technically, you will get an error in generateBlocks because you are creating a null array.

However, you have a for loop that never runs because you are setting i = 1, but then telling the for to run only if i > 9, which will not happen. If you had it to i < 9, you would get an error there because you have a null array.

You need to initialize your array to the proper size in your generateBlocks and fix your for loop to actually run, then it will return a proper array (one with values and not null) which will then fix the other error you are getting as blockBuffer will no longer be null.

1 Like

in GenerateBlocks. you specify that blockArray = null, then try to assign the value at position i.

try this:
Vector3[ ] blockArray = new Vector3[9];

1 Like

Brathnann beat me to it. Additionally, C# Arrays start on index 0 so if you write e.g.

        int blockArraySize = 9
        Vector3[] blockArray = Vector3[blockArraySize]; //initilize with predefined size
        for (int i = 1; i < blockArraySize; i++) {
            blockArray[i] = new Vector3(-9f + 1f, 1f, 0f); //create a new vector3 and subtracting 1 from x pos each iteration to space out the blocks
        }

then element 0 would default to Vector3(0,0,0). (because Vector3 is a value type. if it’d be a reference type like GameObject, it would be null and you’d get a nullpointer again)
instead I guess it should be

 for (int i = 0; i < blockArraySize; i++) {
1 Like

Thanks for all of you, solved every problem, but I got into recursion… :smile:

So basically public Vector3[] blockData; is assigned to blockData = generateBlocks (); and that points to the

public Vector3[] generateBlocks(){ //generate blocks and store it in array
        int blockArraySize = 9;
        Vector3[] blockArray = new Vector3[9];
        for (int i = 0; i < blockArraySize; i++) {
            blockArray[i] = new Vector3(-9 + i, 1f, 0f); //create a new vector3 and subtracting 1 from x pos each iteration to space out the blocks
        }
        return blockArray; //return the info
    }

function, that gets called every updated in

void Update () {
        renderBlocks (blockData); //render blocks with given info in form of array
    }

as I understand. :smile: This problem feels stupidly easy to solve, but how can I go around this phenomenon?:eyes: :eyes:

What is your question? You just don’t want to do renderBlocks in update? Just put it in start after your call to assign values to the array.

1 Like

LOL, now it’s working as expected, probably I just overthinked the situation, ty. :face_with_spiral_eyes: