Object reference not set to an instance of an object world.createRandomWorld
Hi all, sorry to ask but i can't figure this one out.
error = Object reference not set to an instance of an object world.createRandomWorld
script:
public GameObject cube; //assign in the inspector
public GameObject[][][] cubes;
int sizex;
int sizey;
int sizez;
// Use this for initialization
void Start()
{
sizex = 10;
sizey = 10;
sizez = 10;
GameObject[,,] cubes = new GameObject[sizex,sizey,sizez];
createRandomWorld();
}
void createRandomWorld()
{
for (int countx = 0; countx < sizex; countx++)
{
for (int countz = 0; countz < sizez; countz++)
{
for (int county = 0; county < Random.Range(sizey - (sizey - 1), sizey); county++)
{
//cubes[countx][county][countz] = cube; //doesn't work //cubes[countx][county][countz] = (GameObject) cube; //doesn't work cubes[countx][county][countz] = cube as GameObject; //doesn't work } } } }
not sure what happened on that function paste (first time poster so here it is again)
void createRandomWorld()
{
for (int countx = 0; countx < sizex; countx++)
{
for (int countz = 0; countz < sizez; countz++)
{
for (int county = 0; county < Random.Range(sizey - (sizey - 1), sizey); county++)
{
//cubes[countx][county][countz] = cube; //doesn't work
//cubes[countx][county][countz] = (GameObject) cube; //doesn't work
cubes[countx][county][countz] = cube as GameObject; //doesn't work
}
}
}
}
Answer by MayankOctro · Nov 16, 2015 at 09:53 AM
I think you should use public GameObject[,,] cubes;
instead of public GameObject[][][] cubes;
. Second thing you have to change GameObject[,,] cubes = new GameObject[sizex,sizey,sizez];
to cubes = new GameObject[sizex,sizey,sizez];
. Third change cubes[countx][county][countz] = cube;
to cubes[countx,county,countz] = cube;
.
thank you very much $$anonymous$$ayankOctro that fixxedi t
Your answer
Follow this Question
Related Questions
error CS0120: An object reference is required to access non-static member 1 Answer
Object Reference Error 2 Answers
Object reference not set to instance of an object? 1 Answer
Unable to create a reference to an instantiated object and alter it? 1 Answer
Object reference is required to access non-static member? 1 Answer