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. Dismiss Notice

Question How can I reload my game?

Discussion in 'Scripting' started by tomlugin100, Jun 29, 2023.

  1. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    Hello and thank you for reading my post!
    I'm making a 2D game and when the player dies I change the scene to a Game Over screen. On that screen there are two buttons: Play Again and Main Menu. When I press play again there is at least one platform missing (in the game the player - a jelly- jumps from platform to platform) and I get error:
    ***
    MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.GameObject.GetComponent[T] () (at <f712b1dc50b4468388b9c5f95d0d0eaf>:0)
    GameScene.Update () (at Assets/Scripts/GameScene.cs:113)
    ***
    I do indeed destroy GameObjects once they go off-screen and I always thought that Unity destroys all the objects in the previous scene when going to a new one. And in that case, how come Unity is looking for something that was destroyed? Is it possible that Unity skips over the Awake() or Start() methods when I press Play Again?

    The lines Unity poiints to (113 and 94) are verry similar and just try to get the position of a platform. The player dies within a second because theres no platform for him to land on. I don't manually destroy the platform in the new game though. So why is Unity deleting my object(s)?

    Also, there's lava rising up to eat the player. However, it doesn't start rising until the player makes the first move. When I run the game, it works perfectly. When I die and press Play Again the lava starts rising without waiting for the play. I'm just confused as to why restarting the game is different from starting it the first time.
     
    Last edited: Jun 29, 2023
  2. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    public void ResetScene()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    Add this function to the play again button
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    451
    When you call Destroy() on a gameobject, any references to it will point to null after it actually gets destroyed. If you still use any of these references (access any of its fields or methods), you'll get this exception.

    The solution is to add a null check before any use of any gameobject references that point to objects that can possibly be in a state of being destroyed, using just
    if (gameObjectReferenceName) {...}
     
  5. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    Thank you so much for your response! I've found the object that's null and I'm trying to access its position, but I don't understand why it wasn't created. When I run my game for the first time, it creates a lot of platforms (to jump onto) and there's no error. So when I press Play Again, why doesn't the game create all the objects needed just like the first time?
     
  6. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    For me, this code just reloads something that's already loaded. I have the function
    Code (CSharp):
    1. public void loadScene(string sceneName)
    2.     {
    3.         SceneManager.LoadScene(sceneName);
    4.     }
    I tried adding your code as well and, well, everything went haywire
     
  7. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    Thank you everyone for your help! I've solved the problem!
    I had some variables instantiated (I'm not good with using the right terminology, correct me if this sounds off) before the Awake and Start functions and it seems that when the level re-loaded, Unity chose the values of these variables from when the level was over rather than from their instantiated values.
    To solve the problem I just went back and instantiated all variables in the Awake() and Start() functions
     
    Kurt-Dekker and Chubzdoomer like this.