Is there a way to force Awake() to run?

I have a gameObject with a script that I need to be able to carry into the next scene without destroying it, but I would still like to call Awake() on it. Is there a way to do that?

You’re thinking past the problem and incorrectly arriving at Awake().

A better way to think about the problem, assuming I understand you:

  • you want something to happen to an object every time the scene loads.

If this is the case, Awake() isn’t that thing.

Instead look to subscribing to the SceneManager events for when a scene is loaded, such as .sceneLoaded

Remember also the timing diagrams from when we went over Awake() in this thread.

1 Like

I was just thinking that if no one else replies, I bet Kurt will! Thanks for the help as always. I’ll definitely try approaching the problem from another angle.

1 Like

I think the simplest solution for me would be to simply let my gameObject get destroyed at the end of each scene, and then have it created again in the next scene. But I was avoiding doing that because I wasn’t sure how that would affect the memory… it would still be the same exact object, but would Unity recognize it as that, or would it count as creating new instances of the object for each scene, and therefore taking up a lot more memory?

I say it’s the simplest solution for me because my use of Awake() for this object it tied to my use of Awake() for all of my other objects. And changing to some other method for this object would throw of the timing setup since I NEED all of the other objects to definitely be using Awake().

Destroying everything and making it all afresh every scene load is just standard practice.

That’s the point. Everything gets reset.

Here’s some more potentially-relevant reading for you:

Additive scene loading is one possible solution:

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

1 Like

Looks like I have some reading to do :slight_smile: Thanks!