for-loop + array equals "NullReferenceException: Object reference not set to an instance of an object" ? (SOLVED)

Hey Guys I have a huge problem. first of sorry for the german names of my variables.

whenever I run this:

using UnityEngine;
using System.Collections;

public class palme : MonoBehaviour {

    public float[,] m_bomben;
    public bool m_wurfBereit = true;

    public int m_bombenMenge = 1;

    public float m_laengeBlock = 1.0f;

    public int m_laengeMapInBloecken = 11;
    public int m_breiteMapInBloecken = 11;

    System.Random rng = new System.Random();

    public float m_zufallszahlen(int i)
    {
        return (rng.Next(-1 * (i / 2), (i / 2))) * m_laengeBlock;
    }

	void Start () 
    {
      float[,] m_bomben = new float[m_bombenMenge, 2];
    }

	void Update () 
    {
        if (m_wurfBereit)
        {
                for (int i = 0; i < m_bombenMenge; i++)
                {
                    Debug.Log("i ist " + i);
                    m_bomben[i, 0] = m_zufallszahlen(m_laengeMapInBloecken);
                    m_bomben[i, 1] = m_zufallszahlen(m_breiteMapInBloecken);
                    Debug.Log(m_bomben[i,1]);
                }
            }        
            m_wurfBereit = false;
        }
    }

Unity gives me the Error
“NullReferenceException: Object reference not set to an instance of an object”

But I just don’t know why !?
It some how seems to be the Array that messes up.

PS.: I’m pretty new to C# and Unity so there might just be weird coding none would write :slight_smile:

Please add a copy of the error copied from the console to your question. It will give the line number of the error plus other information that is helpful to solve your problem.

You are trying to refer to a variable which is null. Could you post the exact details of the exception?

1 Answer

1

I found the fault ! :slight_smile:

line 25.: float[,] m_bomben = new float[m_bombenMenge, 2];

I’m not setting my m_bomben array, but creating a complete new one in the Start()