Scene works sometimes

I have a SceneB which works fine if I press Play with SceneB active. But it doesn’t work if I load SceneB from SceneA.

If SceneB is run directly I see my debug.Log in my Manager script gets hit once and code which follows works fine, despite seeing a “NullReferenceException: Object reference not set to an instance of an object” on a line which puts some data into an TMPro input field. I see the data in the field in the Game view. (Image at top of the attached pic)
BUT
If SceneB is loaded from SceneA I see my debug.Log in my Manager script gets hit twice and the code which follows doesn’t work, I don’t see the data in the field in the Game view.
(Image at bottom of the attached pic)

I have spent 3 days trying to sort this out and have no idea what to do. HELP!

First, solve you null error! It’s easy to solve and there is a sticky about it!
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

And it points to your OnSceneLoaded, so solve that.

Then, in the second screenshot, it show “Additive” and that you have 2 audio listeners, so you’re loading another scene on top of the first, so you have duplicates of stuff in your scene, possibly even your manager script. So, you’ll need your manager scripts to be designed better, that is they either need to follow a singleton pattern, or you need to make sure they know what to do if they are designed to handle gui and such in the individual scenes.

Or… don’t do additive loading.

I have a Mainscene which on 3 different button clicks launch 3 different scenes, called sceneB, sceneC and sceneD
All are being loaded with the Additive option. sceneB and sceneC work just great and when I exit them I return to the Mainscene perfectly.
All 3 scenes have my Manager script assigned to at least one gameobject in each scene.

If I remove the Additive when I load sceneB or sceneC, they come up, but don’t reach to any button clicks, so the Additive has to stay.

SceneD is the problem one. I found out that while I cannot find an object using Gameobject.Find in order to put data into the input fields I have, I can find them when I exit the scene. I have code which finds those same inputfields and extracts text from them to put into PlayerPrefs.

This makes no sense at all. Why does it not work on entry to the scene, yet works when I am about to exit the scene.

I don’t know much about singletons and don’t really know where I am putting that code. Is it in the Awake of my Manager script?

Additive scene loading is great, but you MUST UNDERSTAND THE TIMING. You can read about it all you like but there’s no substitute for putting in your own Debug.Log() to see what happens and when it happens.

Here’s some more notes:

https://discussions.unity.com/t/820920/2
https://discussions.unity.com/t/820920/4

https://discussions.unity.com/t/824447/2

A multi-scene loader thingy:

https://pastebin.com/Vecczt5Q

My typical Scene Loader:

https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

Other notes on additive scene loading:

https://discussions.unity.com/t/805654/2

Timing of scene loading:

https://discussions.unity.com/t/813922/2

Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It’s a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

Two similar examples of checking if everything is ready to go:

https://discussions.unity.com/t/840487/10

https://discussions.unity.com/t/851480/4

Fix that soonest or it will impede your progress.

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}

There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

FINALLY, here is some timing diagram help:

https://docs.unity3d.com/Manual/ExecutionOrder.html

I sure do appreciate all the effort you have made above, so Thank You. I tried putting the singleton code into my ManagerPlayerPrefs.cs which is attached to a gameobject in the scene also called ManagePlayerPrefs.
The top image is if I include the Awake code and bottom image is if I do not include the Awake code.
These results don’t make sense to me. In the bottom image, you can see that the line of code I used to put the 23 into the input field did succeed, yet I got a NullReferenceException… error.


8394402–1107870–ManagePlayerPrefs.cs (4.29 KB)

I have learned how to use Singletons from youtube videos and got my project working correctly. Thanks for your help Kurt-Dekker.

1 Like