- Home /
I dont know why im getting a null reference exception
My first script makes an array and fills it with objects:
IEnumerator CreateTerrain ()
{
for (int px=0; px<chunkSize; px++)
{
for (int pz=0; pz<chunkSize; pz++)
{
float Perlin1 = Mathf.PerlinNoise((px+ transform.position.x) /xScale, heightScale);
float Perlin2 = Mathf.PerlinNoise((pz+ transform.position.z) /xScale, heightScale);
int x = (int) Mathf.FloorToInt(px + transform.position.x);
int height = (int) Mathf.FloorToInt(Perlin1*40*Perlin2);
int z = (int) Mathf.FloorToInt(pz + transform.position.z);
for (int y=0; y<height; y++)
{
yield return new WaitForSeconds (1/blocksPerSecond);
GameObject currentBlock = GameObject.Instantiate(blocks[0], new Vector3(x, y, z), Quaternion.identity) as GameObject;
currentBlock.transform.parent = transform;
levelData[x, y, z] = currentBlock;
}
}
}
foreach (Transform block in transform)
{
block.GetComponent<BlockScript>().UpdateSides();
}
}
My Script that im getting the error on is:
public void UpdateSides ()
{
levelData = new GameObject[chunkSize, chunkHeight, chunkSize];
levelData = leveldataScript.levelData; *** null reference exception ***
print (levelData.GetLength(0) + " , " + levelData.GetLength(1) + " , " + levelData.GetLength(2));
int x = (int) transform.position.x;
int y = (int) transform.position.y;
int z = (int) transform.position.z;
// it continues.....
iv been trying to figure out why im getting the error but i have no idea. The console prints out: 10, 100, 10 which means that the array isnt null. Can anyone help please thank you
In your code... what is leveldataScript?
levelData = leveldataScript.levelData
The console is printing out values because you have a levelData variable instantiated before being used. I cannot tell if that is public global variable, since I don't see the whole class code. If you could post the definition for both scripts, would be easier to deter$$anonymous$$e the error. $$anonymous$$y gut's tell me that leveldataScript is never initialized.
On which line does the error appear - it should say on the console window, on the error message
Answer by Kiwasi · Dec 15, 2014 at 07:07 PM
Probably because you have a null reference. The error means "something before a dot is null". So go to the error line. Find all the dots. And check everything immediately before a dot has been assigned a value.