Regarding possible Unitys lobby system, when destroying a lobby and all dontdestroyonload gamobjects and loading the main scene from MainGame scene, some scripts that get marked Dontdestroyonload dont work as intended.
My car also does not work as intended. Can you fix it?
You need to provide an error description.
Destroying “instances”, presumably singletons, is ripe with issues in general. Destroying the NetworkManager itself is not recommended at all - I assume this is the object with the NetworkManager script on it. Note that you are calling Shutdown() on that object after you destroyed it.
i moved the shutdown above the Destroy, sadly didnt help.
If I’m correct, Destroy gives you 1 frame to do whatever you want with that gameobject before destroying it.
When reloading the main scene, everything works as normal, except for 1 script (so far, cant test more because that script is needed for other mechanics).
that script enables a piece of UI (gamobject) needed to start the game, it has the correct reference when debugging, but throws a NullrefenenceException when asking it to enable the UI gameobject (only after reloading the scene, the first time it works consistently everytme)
When checking the inspector, its as if the reference always is there, except when the error occurs.
the referenced Gameobject does not get destroyed or moved or anything.
the gameobject gets enabled through an OnSceneLoaded Function:
Do not rely on that! Assume Destroy() means exactly that right away.
Keep in mind the dual nature of Unity objects. Each C# instance of any UnityEngine.Object has a C++ counterpart. The latter may already be null but the former is waiting for the GC to collect it. Hence you sometimes get a MissingReferenceException.
It’s the destroying, particularly destroying singleton instances that will always cause headaches due to the unforseeable order of destroying instances respectively them being collected. Engineer the code in such a way that singleton objects persist throughout the lifetime of the application and you’ll live a happier dev life.
Likely good advice, ill reverse engineer a little bit of code and see how that goes.
I still however do find it interesting that the reference ALWAYS is visble in the inspector, before and even after the nullReferenceException, and Unity even logs a message to the console with in a null check if statement, saying that the object is null, all while the inspector shows the valid correct reference.
if (chooseGodMenu == null)
{
print("null");
}
chooseGodMenu is the gameobject that is null, somehow.
This may not be surprising. The editor has a proxy object that stands in place for display in the GUI, so perhaps this hasn’t been updated yet.
Or the object is destroyed, you receive a nullref, but within a short period of time a new instance of the object is created and assigned so at best you may see it “blink” in the Inspector. Just a guess.