This question was
closed May 28, 2016 at 01:28 PM by
NoobsGoFly for the following reason:
Other, i found the answer
Question by
NoobsGoFly · May 28, 2016 at 05:23 AM ·
arrays2d arraynullreferenceexeption
NullReferenceException in Update()
public Crystal[] crystals;
public Transform Sapphire, Ruby;
private Crystal[,] grid;
// Use this for initialization
void Start()
{
Instantiate(Sapphire, new Vector3(1, 1, 0), Quaternion.identity);
Instantiate(Sapphire, new Vector3(2, 1, 0), Quaternion.identity);
Instantiate(Ruby, new Vector3(3, 1, 0), Quaternion.identity);
Instantiate(Ruby, new Vector3(4, 1, 0), Quaternion.identity);
Crystal[,] grid = new Crystal[9, 9];
crystals = FindObjectsOfType<Crystal>();
foreach (Crystal i in crystals) {
grid[i.xPos, i.yPos] = i;
}
}
// Update is called once per frame
void Update()
{
print(grid[1, 1]);
}
Hey guys im getting a NullReferenceException Error when i try to run this code in the console and the problem is the print statement in Update. For some reason if i put the statement in Start it works fine but i get the error in Update. Can someone tell me why? Thanks!
Comment
Its an int of the x position of the crystal gameobject, sorry if that was not clear before.
yeah but it's a vector so how do you convert from float to int? Anyway i think it maybe because you do this in the Start method. Try it in the Awake see if that helps.