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

Retry button vs Loading next scene

Discussion in 'Scripting' started by Garien, Oct 11, 2018.

  1. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    32
    Hi! This is my first time posting here, despite using this forum a lot yet I couldn't find any solution to my problem. I'll try to make this thread as complete as I can, I'll even add a video. Can someone help? This is what's going on:

    In my game, you can pick up heart containers to increase your max health and coins to add coins to your wallet.

    I have a pause menu with a Restart option. Whenever you click restart, it reloads the current scene. This would work well if I only had one scene.

    After the project moved a bit foward, I had to move on to a next scene. So, I had to use DontDestroyOnLoad() for some objects: PauseUI, GameOverUI, HealthSystem, Wallet and the DontDestroy Game object those stuff are attached to.

    The don't destroy on load script also has a 'Retry' bool, that I created so that whenever you click Restart, you lose the coins and heart containers that you picked up after that point:

    public bool Retry;

    private void Update()
    {
    if (!Retry)
    {

    DontDestroyOnLoad(pauseMenu);

    DontDestroyOnLoad(UICanvas);
    DontDestroyOnLoad(wallet); //This contains all my wallet information.
    DontDestroyOnLoad(gameManager); //This contains all my health information. The others DontDestroyOnLoads are just UI stuff/buttons.
    DontDestroyOnLoad(gameOver);
    DontDestroyOnLoad(pauseSystem);

    DontDestroyOnLoad(quitMenu);


    DontDestroyOnLoad(dontDestroy);


    }
    }

    This works as intended on the first scene. The retry bool is set to true by default, meaning those objects will be destroyed on load, which is okay, since the first scene contains them all. The retry bool is also set to true whenever the scene name isn't the name of the first scene.

    The retry bool is false when you activate the trigger that will take you to the next scene, preventing those objects from being destroyed.

    In the next scene, I deleted those objects from the scene, to avoid the fact that they'd be duplicated since I wasn't destroying them on load.

    When you're in the second scene, if you pick up coins or a heart container and open the PauseMenu and click on Restart... you'll still have the coins, and the container. Meaning you could just pick a container/coin, click restart and pick them up again to have infinite health and money and I don't want that I. However, this shouldn't be happening since I set the destroyonload retry bool to true when you're in the second scene (I checked it on the game object, it is indeed set to true).

    Any tips? Scene transition while saving data between them has been a freaking nightmare for me so far. Any help would be appreciated.

    Here's a video to illustrate it better:

    Everything up to 31 seconds works as intended.

    -------------------------------------------------------------------------------------------
    EXTRA INFO:
    Here's my Scene Transition script:

    private void OnTriggerStay2D(Collider2D collision)
    {
    if (collision.gameObject.CompareTag("Player"))
    {

    if (Input.GetButtonDown("Interact"))
    {
    Debug.Log("Interacted");
    dontDestroy.Retry = false;

    nextSceneTimerCountdown = true;
    }
    if (nextSceneTimer <= 0)
    {
    SceneManager.LoadScene("TG2", LoadSceneMode.Single);
    dontDestroy.Retry = true;
    }

    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You could just not dontdestroyonload everything and simply transfer data over and use that to redo your gui. Otherwise, you're going to have to track what you pick up in the new scene and if restart is hit, remove whatever you picked up.

    So at the start of the new scene, have some variables that track what you get and reset those to 0. If I pick up a coin, increment that variable along with your total. If I restart, you know how many coins to subtract from the total and then reset the variable tracking what you get in the scene.
     
  3. zakdank

    zakdank

    Joined:
    Jan 25, 2011
    Posts:
    46
    Pretty much what the above poster said.

    Another way would be to use Start in the new scene to set your previous variables like score and then have the restart method set your score to that variable. This way you dont need a counter or to subtract anything.
     
    Last edited: Nov 12, 2018
  4. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    32
    Thank you for the reply. I tried this, but it still doesn't work (I made sure the ''Retry bool'' is true so that everything gets destroyed if I click on Restart, just like on the first scene). Watching the hierarchy while playing, I noticed that when I move into scene 2, there's a new ''scene'' called DontDestroyOnLoad, like this:
    upload_2018-10-11_12-46-18.png

    I may be missing something, I checked everything and things should be working (since all settings are identical to the first scene now), but they're not.
    Before I try to re-check everything for like, the 5th time, could you confirm if my theory is true?
    My theory is that everything within the DontDestroyOnLoad ''Scene'' will never be destroyed, even if the 'Retry' bool that prevents everything from being destroyed is set to true.
     
    Last edited: Oct 11, 2018
  5. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    32
    Thanks for the reply! Would that also work with the checkpoints I make during my scene, as long as I record the current value of the coins whenever the player triggers the checkpoint, right?
     
  6. zakdank

    zakdank

    Joined:
    Jan 25, 2011
    Posts:
    46
    Yes, just have it set the variables to the current score/coins.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Gameobjects that are set to dontdestroyonload are not destroyed by scene changes.
     
  8. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    32
    I tried that and it works. My checkpoints are working fine on the second scene and on... But they're not working on the first scene since all my game objects already exists in that scene.

    One solution I found was to change how the game level selector will work, without any coding changes (and I came up with a pretty nice idea, thanks to that), but I still want to have the code knowledge. Any ideas on how I would make my checkpoints work on the very first scene using your method?
     
  9. zakdank

    zakdank

    Joined:
    Jan 25, 2011
    Posts:
    46
    I'm not sure what you mean, are you saying it isn't working because there is no previous coin/score for the variable to set to? You could just do
    Code (CSharp):
    1.  
    2. if (coinVariable != 0)
    3. {
    4. //set variable to current score/coins
    5. }
     
    Garien likes this.
  10. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    32
    I believe this will work. Thank you for your help. If you want to get a free copy of the game once it's released, send me your e-mail via inbox and I'll give you a copy :)