Question by
sysmaya · Aug 03, 2016 at 01:12 PM ·
arraysinitializedeclare
Error Initialize array two dimensions C#
First Class:
using UnityEngine;
using System.Collections;
public class Slot : MonoBehaviour {
public string State= "VACIO";
public string Player;
}
Second Class:
using UnityEngine;
using System.Collections;
public class ControlGame : MonoBehaviour {
public Slot[,] gridSlot = null;
void Start () {
gridSlot = new Slot[3,3];
gridSlot [0, 0].State = "FULL";
gridSlot [0, 0].Player= "TOWER";
}
}
Compilig error: NullReferenceException: Object reference not set to an instance of an object ControlGame.Start () (at Assets/scripts/ControlGame.cs:8)
Comment
$$anonymous$$e Answer 1 hour later ...
using UnityEngine;
using System.Collections;
public class ControlGame : $$anonymous$$onoBehaviour {
public Slot[,] gridSlot = null;
void Start () {
gridSlot = new Slot[3,3];
gridSlot [1, 1] = new Slot (); /* Initialize AGAIN */
gridSlot [0, 0].State = "FULL";
gridSlot [0, 0].Player= "TOWER";
}
}
This is how standard NEWing really works -- where you wrote "Initialize Again" isn't really initializing again. It's doing the rest of the work. You can read about this in just regular places that explain C# (or Java) arrays and classes and NEW.
Your answer
Follow this Question
Related Questions
Array of Arrays 3 Answers
Array cannot read Greek characters 1 Answer
Randomize text position for 2D Quiz C# 0 Answers
Getting the closest GameObject to the player from an array 1 Answer
only keep every 10th entry in an array? 2 Answers