- Home /
Object reference not set to...
Object reference not set to an instance of an object in line 15 where theworld[j,k,i] = (byte)(0);:
using UnityEngine;
using System.Collections;
public class WORLD : MonoBehaviour {
byte[,,] theworld = new byte[1000,1000,1000];
public Transform rock;
void Start () {
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000; j++)
{
for(int k = 0; k < 3; k++)
{
theworld[j,k,i] = (byte)(0);
}
}
}
Generate(Random.seed);
}
void Update () {
}
void Generate(int seed)
{
for(int i = 1; i < 1000; i++)
{
for(int j = 1; j < 1000; j++)
{
int height = Random.Range(1, 2);
for(int k = 1; k < height+1; k++)
{
theworld[j,k,i] = (byte)(1);
}
}
}
for(int i = 1; i < 1000; i++)
{
for(int j = 1; j < 1000; j++)
{
for(int k = 1; k < 3; k++)
{
if(theworld[j+1,k,i] == (byte)(0) ||
theworld[j,k,i+1] == (byte)(0) ||
theworld[j-1,k,i] == (byte)(0) ||
theworld[j,k,i-1] == (byte)(0) ||
theworld[j,k+1,i] == (byte)(0) ||
theworld[j,k-1,i] == (byte)(0)
)
{
Instantiate (rock, new Vector3(j,k,i), Quaternion.identity);
}
}
}
}
}
}
Answer by whydoidoit · Jan 26, 2013 at 09:37 AM
I'm guessing it isn't allowing you to allocate 1GB of RAM to that array. If your world is going to be sparsely populated you can use a Dictionary and a lookup function which will give you O(1) performance but a lower memory overhead.
Possible, it writing me about ended RA$$anonymous$$, but it using only 2,5 Gb of the RA$$anonymous$$ and still working pretty much.
$$anonymous$$ake the array dimensions smaller and see if you still get the null reference.
Your answer
Follow this Question
Related Questions
Script decides not to run anymore 1 Answer
Object reference not set to an instance of an object. 2 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Object reference not set to an instance of an object. 1 Answer
An object reference is required to access non-static member `UnityEngine.Component.transform' 1 Answer