0
\$\begingroup\$

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 ?

\$\endgroup\$
  • 1
    \$\begingroup\$ I am not getting any error on runtime, but when I play in unity it is giving me error What do you mean by that? Is that not at runtime? \$\endgroup\$ – Raimund Krämer Jan 29 '18 at 9:04
  • \$\begingroup\$ Please add more code, at least everything that is referenced in the snippet, like `initPosX`` . \$\endgroup\$ – Raimund Krämer Jan 29 '18 at 9:13
5
\$\begingroup\$

I dont understand Unity and MonoDevelop, bud I think, you don't have objects in array intialized. You created array, but objects in it are null. You have to call new GridInfo on each of them.

Somethink like:

foreach (GridInfo gi in Tile) // I now don't know, if this goes over 2D array too
{
   gi = new Grid();
}
\$\endgroup\$
  • \$\begingroup\$ hey thnx..it wrking nw.. \$\endgroup\$ – Syed Nov 22 '11 at 9:54
  • 1
    \$\begingroup\$ To be precise, foreach won't work in this case because C# doesn't allow assignments to the iteration variable. A for( ; ; ) loop or other means is needed instead. \$\endgroup\$ – DuckMaestro Nov 22 '11 at 23:27
0
\$\begingroup\$

To give an alternative to zacharmarz's answer:

The way that I think you intend to use the GridInfo is possible, if you change it to a struct instead of a class. If you do, then you can't inherit from MonoBehaviour, but I think that you might do that unintentionally because Unity automatically inserts that code when creating the script. Since the GridInfo is just a structure of multiple public data fields, it is well suited for using a struct.

Note that I'm not sure if it is possible to have a struct of the same type inside the struct, as in the case of the field parent.

It would look like this:

public struct GridInfo {
    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;        
}

Then a 2D-array of structs will automatically contain the structs with all their fields. There are no nullpointers of type struct.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.