My reason for not using the instantiate function is because i dont want to use prefabs, so i’m creating the object from scratch. The odd thing is that it tells you the object that is created (Sun) and its ID (-1988) but right below the print lines i ask it to add a parent, which it doesnt do since theres no reference to the object it just told it it made?
Is it an issue with IDs or is it something else?
The code is below (the generate sun function simply returns a game object)
var generatedSun = CelestialBodyGenerator.GenerateSun();
print(generatedSun);
print(generatedSun.GetInstanceID());
generatedSun.transform.parent = sunsRoot.transform;
solarSystemScript.suns.Add(generatedSun);
what type of Class is this generatedSun object?
If it doesn’t inherits from GameObject it will not have the transform thing so your code will fail
1 Like
while its not 100% clear which line is the issue, assuming the first 3 lines in console are the 2 print lines, and something else, it would say solarSystemScript or suns is null maybe
its a GameObject, nothing special
Make sure sunsRoot
, solarSystemScript
, and solarSystemScript.suns
are not null. Use your IDE’s debugger and see what the values of everything are at that point in the code.
the solarSystemScript is what the first console line refers to, so that isnt null. suns is a public gameobject list inside that script so i have no clue what the issue is
did so, everything has a value except solarScript.suns, which is expected since its an empty list
Which line is throwing the exception? If it’s the line with the Add
call, the most likely thing is that the list is null (if you’re sure solarSystemScript
is not null). There’s a difference between an empty list and null. It’s extremely unlikely that you would get the exception otherwise.
the line throwing the exception is line 93 of my script, which is the “solarScript.suns.Add” line
when i inserted a print for the list it returned Null, is that not the same as an empty list? sorry this is my first time trying something like this
null
means there is no list object at all. You need to make sure a list is created beforehand, like in an Awake method if solarSystemScript
is a MonoBehaviour (or a constructor if solarSystemScript
is a normal, non-Unity object).
suns ??= new List<GameObject>();
(??=
means create/assign if and only if the variable was already null)
1 Like
this was it! thank you so much, this had me stumped for a while haha. thanks to everyone for answering and being patient ^^