Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Editor & General Support' started by IIBearWithII, Jun 5, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,132
    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.
     
    IIBearWithII likes this.
  3. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    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.
     
    Kurt-Dekker likes this.
  4. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    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().
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,132
    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://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    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://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    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://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794
     
    IIBearWithII likes this.
  6. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Looks like I have some reading to do :) Thanks!