scene running start/onEnable before it is enabled

I’ve got some code in a scene in the start function.
I’m expecting this code to not run until I load that scene with SceneManager.LoadScene from another scene.

However, it seems to run when I start my game.
Is it possible to stop this firing until I actually call the scene? What function do I need to use for that?

Thanks

Hi newcoder17,

You must have that script attached to something in the starting scene. From the unity documentation - Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.

Also, as it will be a new instance in your later scene, then does it really matter that it was called on an old instance in a previous, startup, scene?

Let me try and explain.

I have a loading screen (scene A) that does licence checking.
If licence is OK it loads up scene B, the main game.

As part of the game I have a need to basically reset everything. The easiest way to do that is to call a scene C, which is a copy of scene A without the licence checking, so all it does is call scene B again, (thus reloading the game)

So at present, when the game starts scene C is loading scene B before scene A has had a chance to even do the licence checking.

So yes, it does matter.

Is there not a method I can put the code in in scene C which will not fire until I switch to that scene using SceneManager.LoadScene?

P.S. yes, the code is attached to an object on scene C, as I want it to fire when scene C is switched to, but not before.

Apologies, I must be on a slow day today - not sure I am following that correctly. :face_with_spiral_eyes:

As I understand it, your scenes are run in sequence like this: A => B => C => B => C etc. That is to say, A is called only once at the start.

Your scene A does the licence check and that is necessary to complete before the game is played in scene B. Now at some points, after or during the game, scene C is called (and is without the licence checking) to restart scene B.

So I am thinking that the code that needs to run is in scene A and must be stopped in scene C. But you are asking for the code to run in scene C but not in scene A?

To answer this directly, you need to switch the logic around. That is to say, mark the script in the editor as disabled. The Start() method will now not be called. When you are in a scene and you want that code to run then something in that scene will need to enable that script. Then the Start() method will be called.

Yes, I tried this after my last post.

But I cannot access the object from scene b to enable it. It won’t let me drag and drop it to a public gameobject variable, presumably as it is on a different scene.

Just create a simple script that enables the target script. Then place that simple script on a gameobject that is in scene C but not in scene A.

As an alternative, you could try having the script enabled but add an Awake() method. Then in Awake(), check the current scene. If it is scene A, then disable the script (that should prevent Start() from being called) else do nothing which will mean Start() does get called.

Thanks for your help, but I still dont think this will work.
I believe the ultimate issue is that the code seems to get called in all scenes, whether they are enabled or not.

So :
Scene A - checks licence and loads scene b
Scene C - loads scene B

“Just create a simple script that enables the target script. Then place that simple script on a gameobject that is in scene C but not in scene A.”

wont work, as on scene C it will be called on startup and will be enabled.

The alternative may work, I’ll have a play.

Doh!
I’m a fool.

I’d left Scene C loaded in the Hierarchy, which is of course why it was loading it at start!
I’ve unloaded it, and its working now, and only firing when I specifically call it!

Sorry for wasting your time.

1 Like

Ah ok, that makes sense.

No worries. Glad you got it fixed. :slight_smile: