Skip to main content
added 52 characters in body
Source Link
Tetrad
  • 30.1k
  • 12
  • 96
  • 143

Unable to access A class NullReferenceException when accessing variables in B Class -a 2D array in Unity-Monodevelop

I have made a class including variables in Monodevelop which is:

public class GridInfo : MonoBehaviour {

    public float initPosX;
    public float initPosY;
    public bool inUse;
    public int f;
    public int g;
    public int h;
    public GridInfo parent;
    public int  y,x;        
}

Now I am using its class variable in another class, Map.cs which is:

 public class Map : MonoBehaviour {

    public static GridInfo[,] Tile = new GridInfo[17, 23];

    void Start() {

        Tile[0,0].initPosX = initPosX;  //Line 49
            }
}

}

Iam I am not getting any error on runtime, but when I play in unity it is giving me error

NullReferenceException: Object reference not set to an instance of an object
Map.Start () (at Assets/Scripts/Map.cs:49)

I am not inserting this script in any gameobject, as Map.cs will make a GridInfo type array, I have also tried using variables using GetComponent, where is the problem ?

Unable to access A class variables in B Class - Unity-Monodevelop

I have made a class including variables in Monodevelop which is:

public class GridInfo : MonoBehaviour {

public float initPosX;
public float initPosY;
public bool inUse;
public int f;
public int g;
public int h;
public GridInfo parent;
public int  y,x;        
}

Now I am using its class variable in another class, Map.cs which is:

 public class Map : MonoBehaviour {

 public static GridInfo[,] Tile = new GridInfo[17, 23];

void Start() {

    Tile[0,0].initPosX = initPosX;  //Line 49
            
}

}

Iam not getting any error on runtime, but when I play in unity it is giving me error

NullReferenceException: Object reference not set to an instance of an object
Map.Start () (at Assets/Scripts/Map.cs:49)

I am not inserting this script in any gameobject, as Map.cs will make a GridInfo type array, I have also tried using variables using GetComponent, where is the problem ?

NullReferenceException when accessing variables in a 2D array in Unity

I have made a class including variables in Monodevelop which is:

public class GridInfo : MonoBehaviour {

    public float initPosX;
    public float initPosY;
    public bool inUse;
    public int f;
    public int g;
    public int h;
    public GridInfo parent;
    public int  y,x;        
}

Now I am using its class variable in another class, Map.cs which is:

public class Map : MonoBehaviour {

    public static GridInfo[,] Tile = new GridInfo[17, 23];

    void Start() {

        Tile[0,0].initPosX = initPosX;  //Line 49
    }
}

I am not getting any error on runtime, but when I play in unity it is giving me error

NullReferenceException: Object reference not set to an instance of an object
Map.Start () (at Assets/Scripts/Map.cs:49)

I am not inserting this script in any gameobject, as Map.cs will make a GridInfo type array, I have also tried using variables using GetComponent, where is the problem ?

Source Link
Syed
  • 959
  • 3
  • 15
  • 28

Unable to access A class variables in B Class - Unity-Monodevelop

I have made a class including variables in Monodevelop which is:

public class GridInfo : MonoBehaviour {

public float initPosX;
public float initPosY;
public bool inUse;
public int f;
public int g;
public int h;
public GridInfo parent;
public int  y,x;        
}

Now I am using its class variable in another class, Map.cs which is:

 public class Map : MonoBehaviour {

 public static GridInfo[,] Tile = new GridInfo[17, 23];

void Start() {

    Tile[0,0].initPosX = initPosX;  //Line 49
            
}

}

Iam not getting any error on runtime, but when I play in unity it is giving me error

NullReferenceException: Object reference not set to an instance of an object
Map.Start () (at Assets/Scripts/Map.cs:49)

I am not inserting this script in any gameobject, as Map.cs will make a GridInfo type array, I have also tried using variables using GetComponent, where is the problem ?