Search Unity

DontDestroyOnLoad for that scene only?

Discussion in 'Editor & General Support' started by conan222, May 28, 2019.

  1. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Hi. I have a scene that represents a map graphic and has 15 toggles on it. Each toggle sends you to another scene when clicked and the toggle uses a custom green ball sprite which changes to a red ball sprite once it's clicked.

    The idea is that people can see which toggle they've already clicked on so don't need to click it again.

    All I've done is attached the code below to the toggle.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DontDestroyOnLoad : MonoBehaviour
    6. {
    7.     void Awake()
    8.     {
    9.         DontDestroyOnLoad(transform.gameObject);
    10.     }
    11. }
    12.  

    Problem is once I've clicked a toggle and it's gone red, off I go to that new scene as planned but the red toggle follows. I'd like it to remain in it's original scene only so when I come back to it (the map scene) I can see it in place and red.

    Could anyone please advise me on how to accomplish this?

    Thanks for your time,

    Andrew
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    If you add DontDestroyOnLoad flag to an object, nothing is stopping you from destroying the object yourself if circumstances warrant. When you load a scene where this object should not be used, deactivate or destroy it.
     
  3. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Thanks very much Halley. Appreciate it.

    I've tinkered with Destroy on a few of the scenes and your right it doesn't appear. Problem I have now is that when I return to the map scene the toggle state 'Red' isn't saved and it's back to green even though it's been clicked on.

    I was hoping the DontDestroyOnLoad would retain the toggle state also :confused:
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,443
    As I hinted, "deactivate OR destroy."

    red_thingy.SetActive(false);


    Later,

    red_thingy.SetActive(true);
     
  5. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Thanks. I did also look up deactivate after your recommended it but things appeared to get too complicated

    Here's the code I used for it:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DeactivateMarkers : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Marker1;
    9.     public GameObject Marker2;
    10.  
    11.  
    12.     void Start()
    13.  
    14.     {
    15.         // There will be a total of 15 markers but just testing with 2
    16.         Marker1.SetActive(false);
    17.         Marker2.SetActive(false);
    18.  
    19.     }
    20. }

    The problem arises when I apply this to one of the other scenes I still need to reference the two gameobjects that need to be deactivated but I can only choose gameobjects from the current scene. The markers are in another scene.

    Screenshot attached of the inspector.


    Nothing is easy in Unity ;-)
     

    Attached Files:

  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Use PlayerPrefs to save the state of the toggles. When the scene loads lookup the state saved in PlayerPrefs and set it appropriately.

    The benefit of PlayerPrefs or some other save file is that the button states will remain even after the player quits the game entirely.
     
  7. conan222

    conan222

    Joined:
    Dec 15, 2018
    Posts:
    26
    Thank you Antony. I will also look up some PlayerPrefs beginner tutorials as I'm not familiar with them. The idea of it sounds like it might work.