-2

So i am trying to make a simple gameobject saving system in Unity.

Whenever i place an cube i enter it in a GameObject called currentCube

GameObject currentCube = new GameObject();
currentCube = Instantiate(CubeBlue, placeLocation, new Quaternion());

This places my cube all good and well. Then just for debugging i check if the currentCube actually has the just placed Cube

print(currentCube.transform.position);

This does indeed give the location of my newly placed Cube. But then i try to add the cube to a List (also tried with an array)

cubes.Add(currentCube);

But then i get a NullReferenceException

NullReferenceException: Object reference not set to an instance of an object

And then with the Script filename and the location line of where i put the "cubes.Add(currentCube;)"

EDIT:

Also in my part above the void start i have stated private List<GameObject> cubes;

  • Why do you think `currentCube` is the problem for the NullReferenceException you got at `cubes.Add(currentCube)`? Did you verify in the debugger that `currentCube` is actually null when executing this code line? And why would you think that it would not be possible to add a `null` value to the list (and this causing a NullReferenceException, as you seem to believe)? –  Sep 19 '22 at 11:59
  • Where do you initialize `cubes`? Based on the code you've provided, that seems more likely to cause the NRE. – gunr2171 Sep 19 '22 at 12:02
  • @MySkullCaveIsADarkPlace well that is just what looks like the problem to me, what i forgot to add is that i have this in my class as well private List cubes; Im not that known to using Lists but this looked like the problem to me atleast – Bloemenpot Sep 19 '22 at 12:04
  • @gunr2171 I indeed forgot to mention that, i edited it now. But i have it at the top of my code as private List cubes; – Bloemenpot Sep 19 '22 at 12:06
  • That's where you _declare_ the variable, where do you _initialize_ `cubes`? – gunr2171 Sep 19 '22 at 12:08
  • @gunr2171 ah, it seems i indeed missed the part of initializing the list. thats where the problem was. i just added "cubes = new List();" and now i no longer get the error and when checking the amount of objects in the list it indeed works now – Bloemenpot Sep 19 '22 at 12:13
  • 1
    Here's more info about that problem. [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – gunr2171 Sep 19 '22 at 12:13
  • @gunr2171 Oh that looks like quite a handy read to look through. For now you fixed my problem but i will definitely give that page a read since it looks quite handy for in the future – Bloemenpot Sep 19 '22 at 12:15
  • You know that `GameObject currentCube = new GameObject(); currentCube = Instantiate(CubeBlue, placeLocation, new Quaternion());` will actually create **two** new objects in your scene right? – derHugo Sep 19 '22 at 12:59

1 Answers1

1

Looks like your List is not getting Initialized. Try

private List<GameObject> cubes = new List<GameObject>();
Shemberle
  • 31
  • 2
  • he did not initialized, do give him the knowledge too of why initialization is important :), giving you one upvote for picking up one of the possibility of solution (could be only but still) – ARr0w Sep 19 '22 at 12:44