How do I get a U.I. image to remember if it is active or Not Active Across Scene Changes Using Arduino

I have part of my interface on a slide in animated panel on a canvas that lights up a indicator light (U.I. button)
Now if I set it for open the indicator light switches switches to active and the other indicator switches to un active via a manager C# script.

Now how do I retain that data across scene changes?

I tried putting the Don’t destroy on load on my manager script but weird things happen. For one it does not carry the data across scene changes, secondly if I remove the Don’t destroy on load stuff to put my script back to normal then my indicators don’t respond to the make active and not active portion of the IEnumerator part of the script.

How can I fix this?

Here is a portion of my script that has the Autodoors set active and un active functions.

NOTE: I placed some images in the comments below of my inspector and the U.I. Panel that has the indicator lights on it that I am wanting to remember their settings when I leave that main scene and go to other scene and then return to the main scene.

//Auto Door Left
	public void  GoAutoDoorLeft(){
		StartCoroutine(AutoDoorL());
		//Star Indicator Light On Coroutine
		StartCoroutine(AutoDoorLindicatorOn());
	}

	//Indicator L On Initiator
	IEnumerator AutoDoorLindicatorOn(){
		yield return new WaitForSeconds (2.0f); // wait time
		//initiate Indicator light On
		AutoDoorOpenLightOnLeft.SetActive(!AutoDoorOpenLightOnLeft.active);
		AutoDoorCloseLightOnLeft.SetActive(!AutoDoorCloseLightOnLeft.active);
		GetComponent<AudioSource>().PlayOneShot(DTMFtone01);
	}
	
	IEnumerator AutoDoorL(){
		yield return new WaitForSeconds (0.5f); // wait time
		anim.enabled = true;
		//play the Slidein animation
		anim.Play("AutoDoorsIndicatorsSlideInAni");
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideInFX);
	}

	//Auto Door Screen Slide Out
	public void  AutoDoorSlideOut(){
		StartCoroutine(AutoDoorOut());
	}
	
	IEnumerator AutoDoorOut(){
		yield return new WaitForSeconds (3.0f); // wait time
		anim.enabled = true;
		//play the SlideOut animation
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideOutFX);
		anim.Play("AutoDoorIndicatorsClosed");

	}

I’m not sure which other logic is coupled with those indicators, you have to make sure the logic applies correctly according to the restored state.

This is just a simple example, using a dictionary to store per-session states and preferences without spamming PlayerPrefs with junk. If you need to retain the data across several starts of the application, you may want to adjust this a little bit.

It’s actually just a dictionary wrapped in another class which handles some situations and avoid ugly casts inside your script, this will already do it (unless you save, e.g. a boolean and try to read the value as int or something, of course that’s not gonna work). Perhaps you should add some exception handling. You might also want to add more methods like remove entries and so on.

static class StateDictionary
{
    private static Dictionary<string, object> myStates = new Dictionary<string, object>();

    public static bool GetState<T>(string key, out T state)
    {
        state = default(T);
        object restoredState = null;
        if(myStates.TryGetValue(key, out restoredState))
        {
            state = (T)restoredState;
            return true;
        }
        return false;
    }

    public static void AddOrReplaceState(string key, object value)
    {
        if (myStates.ContainsKey(key))
        {
            myStates[key] = value;
            Debug.Log("state replaced");
        }
        else
        {
            myStates.Add(key, value);
            Debug.Log("state added");
        }
    }
}

So, how to use that? I’m not entirely sure how scene loading is handled in your game (trigger, UI, time […]), but here’s an example:

In your scene, you should now have an object that is addressed whenever you want to load a scene. This will use the following script. Possible setup: Imagine you’ve got a button in order to load the next scene, so add the LoadScene method to the onClick event in the inspector or associate it with the existing logic in your project.

public class SceneLoader : MonoBehaviour {

    public delegate void OnExitSceneEventHandler();
    public event OnExitSceneEventHandler onExitScene;

	public void LoadScene(int index)
    {
        if (onExitScene != null)
            onExitScene();
        Application.LoadLevel(index);
    }
}

And finally, some test object:

public class Example : MonoBehaviour
{
    // the object you want to save states from
    public GameObject myGO;
    // a script that handles loading the next scene
    public SceneLoader sceneLoader;

    // just a test key
    private string testKey = "rendererActivated";

    void Awake()
    {
        // subscribe to the event which the scene loader raises 
        // before it loads the next level
        sceneLoader.onExitScene += SaveState;

        // store the tmporary state here, maybe there is none so 
        // we don't want to apply it directly
        bool tmp;
        if(StateDictionary.GetState<bool>(testKey, out tmp))
            myGO.SetActive(tmp); // we found a state, let's apply it
    }

    // method that will be called by the event as soon as it's raised
    // so put every save-state logic into it
    private void SaveState()
    {
        // just saving the active state (boolean) for a testing purpose
        StateDictionary.AddOrReplaceState(testKey, myGO.activeSelf);
    }
}

Thanks Suddoha,
I’ll see if I can do a test scene and see how your solution works, but just to be clear on how those indicator lights are activated I’ll post my entire script, it should be at the top of the script the information you were wondering about.

“I’m not sure which other logic is coupled with those indicators”

using UnityEngine;
using System.Collections;

public class ThreeDOverviewManagerScript : MonoBehaviour {


	//refrence for the Auto Doors Indicator panel in the hierarchy
	public GameObject AutoDoorPanel;
	public GameObject AutoDoorOpenLightOnLeft;
	public GameObject AutoDoorOpenLightOnRight;
	public GameObject AutoDoorCloseLightOnLeft;
	public GameObject AutoDoorCloseLightOnRight;
	//animator reference
	private Animator anim;
	//variable for checking if the game is paused 
	//private bool isPaused = false;


	public AudioClip Scanner;
	public AudioClip TurboWarning;
	public AudioClip TurboBoost;
	public AudioClip AutoDoorSlideInFX;
	public AudioClip AutoDoorSlideOutFX;
	public AudioClip DTMFtone01;
	public AudioClip DTMFtone02;

	void Start () {
		//unpause the game on start
		//Time.timeScale = 1;
		//get the animator component
		anim = AutoDoorPanel.GetComponent<Animator>();
		//disable it on start to stop it from playing the default animation
		//anim.enabled = false;
	}

	void Update () {
		
		if (Input.GetButtonDown ("Fire1")) {
			//clicked elsewhere on screen
			Debug.Log ("Clicked Screen");
		}
	}
		
		
	//Plays Auto Door Screen Slidein Sound FX
	public void AutoDoorSlideInSoundFX()
	{
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideInFX);
		
	}

	//Plays Auto Door Screen SlideOu Sound FX
	public void AutoDoorSlideOutSoundFX()
	{
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideOutFX);
		
	}


	public void TurboBoostSound()
	{
		GetComponent<AudioSource>().clip = TurboBoost;
		GetComponent<AudioSource>().PlayOneShot(TurboBoost);
		if(GetComponent<AudioSource>().isPlaying)
		{
			GetComponent<AudioSource>().Stop();
		}
		else
		{
			GetComponent<AudioSource>().Play();
		}

		
	}


	public void TurboWarningSound()
	{
		GetComponent<AudioSource>().clip = TurboWarning;
		GetComponent<AudioSource>().PlayOneShot(TurboWarning);
		if(GetComponent<AudioSource>().isPlaying)
		{
			GetComponent<AudioSource>().Stop();
		}
		/*else
		{
			audio.Play();
		}*/

	}


	public void ScannerSound()
	{
		StartCoroutine(LoadS1("ScannerAdjustScreen"));
		GetComponent<AudioSource>().clip = Scanner;
		GetComponent<AudioSource>().loop = true;
		if(GetComponent<AudioSource>().isPlaying)
		{
			GetComponent<AudioSource>().Stop();
		}
		else
		{
			GetComponent<AudioSource>().Play();
		}
	}

	IEnumerator LoadS1(string ScannerAdjustLev){
		yield return new WaitForSeconds(3.5f); // wait time
		Application.LoadLevel(ScannerAdjustLev);
		
	}


	public void  GoSurveillance(){
		StartCoroutine(LoadT1("SurveillanceModeScreen"));
	}
	
	IEnumerator LoadT1(string level01){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level01);
		
	}

	public void  GoMusicPlayer(){
		StartCoroutine(LoadT2("KITT_MusicPlayer"));
	}
	
	IEnumerator LoadT2(string level03){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level03);
		
	}

	public void  GoHomeScreen(){
		StartCoroutine(LoadT3("KnightSpinningLogo_NoKITTintro"));
	}
	
	IEnumerator LoadT3(string level03){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level03);
		
	}

	public void  GoAutoDWH(){
		StartCoroutine(LoadT4("AutoWindowSDoorsHatchLightsController"));
	}
	
	IEnumerator LoadT4(string level04){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level04);
		
	}

	public void  GoAirVac(){
		StartCoroutine(LoadT5("OxygenSupplyScreen"));
	}
	
	IEnumerator LoadT5(string level05){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level05);
		
	}

	public void  GoSystemGuidance(){
		StartCoroutine(LoadT6("SystemGuidanceScreen"));
	}
	
	IEnumerator LoadT6(string level06){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level06);
		
	}

	public void  GoComLinkClock(){
		StartCoroutine(LoadT7("KnightRiderComLinkClock"));
	}
	
	IEnumerator LoadT7(string level07){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level07);
		
	}

	public void  GoAnalyzer(){
		StartCoroutine(Analyzer01("Analyzer"));
	}
	
	IEnumerator Analyzer01(string analyzer){
		yield return new WaitForSeconds (0.5f); // wait time
		Application.LoadLevel (analyzer);
	}

	public void  GoVoiceBox(){
		StartCoroutine(VoiceBox01("VBscene"));
	}
	
	IEnumerator VoiceBox01(string VoiceBox){
		yield return new WaitForSeconds (0.5f); // wait time
		Application.LoadLevel (VoiceBox);
	}

	public void  GoTemperature(){
		StartCoroutine(Temperature01("InteriorTemperatureScreen"));
	}
	
	IEnumerator Temperature01(string Temperature){
		yield return new WaitForSeconds (0.5f); // wait time
		Application.LoadLevel (Temperature);
	}

	public void  GoLaser(){
		StartCoroutine(Laser01("LaserChargeScreen"));
	}
	
	IEnumerator Laser01(string Laser){
		yield return new WaitForSeconds (0.5f); // wait time
		Application.LoadLevel (Laser);
	}

	public void  GoMicroJam(){
		StartCoroutine(MicroJam01("MicroJamScene"));
	}
	
	IEnumerator MicroJam01(string MicroJam){
		yield return new WaitForSeconds (0.5f); // wait time
		Application.LoadLevel (MicroJam);
	}

	public void  GoAnharmonicSynth(){
		StartCoroutine(LoadAnSynth("AnharmonicSynthesizerScene"));
	}
	
	IEnumerator LoadAnSynth(string level03){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(level03);
		
	}

	//Spare Button
	public void  GoMultipleWebCams(){
		StartCoroutine(LoadWebCams("MultipleWebCameras"));
	}
	
	IEnumerator LoadWebCams(string MultiWebCams){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(MultiWebCams);
		
	}
	//End Spare button

	//Slide Show Button
	public void  GoSlideShow(){
		StartCoroutine(LoadSlideShow("SlideShow"));
	}
	
	IEnumerator LoadSlideShow(string Slideshow){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(Slideshow);
		
	}
	//End Slide Show button

	//Alpha Circuit Button
	public void  GoAlphaCircuit(){
		StartCoroutine(LoadAlphaCircuit("KITTsAlphaCircuitScan"));
	}
	
	IEnumerator LoadAlphaCircuit(string Alpha){
		yield return new WaitForSeconds(0.5f); // wait time
		Application.LoadLevel(Alpha);
		
	}
	//End Alpha Circuit button


	//Auto Door Left
	public void  GoAutoDoorLeft(){
		StartCoroutine(AutoDoorL());
		//Star Indicator Light On Coroutine
		StartCoroutine(AutoDoorLindicatorOn());
	}

	//Indicator L On Initiator
	IEnumerator AutoDoorLindicatorOn(){
		yield return new WaitForSeconds (2.0f); // wait time
		//initiate Indicator light On
		AutoDoorOpenLightOnLeft.SetActive(!AutoDoorOpenLightOnLeft.active);
		AutoDoorCloseLightOnLeft.SetActive(!AutoDoorCloseLightOnLeft.active);
		GetComponent<AudioSource>().PlayOneShot(DTMFtone01);
	}
	
	IEnumerator AutoDoorL(){
		yield return new WaitForSeconds (0.5f); // wait time
		anim.enabled = true;
		//play the Slidein animation
		anim.Play("AutoDoorsIndicatorsSlideInAni");
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideInFX);
	}

	//Auto Door Screen Slide Out
	public void  AutoDoorSlideOut(){
		StartCoroutine(AutoDoorOut());
	}
	
	IEnumerator AutoDoorOut(){
		yield return new WaitForSeconds (3.0f); // wait time
		anim.enabled = true;
		//play the SlideOut animation
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideOutFX);
		anim.Play("AutoDoorIndicatorsClosed");

	}

	//////////////////////////////////////////////////
	//Auto Door Right
	public void  GoAutoDoorRight(){
		StartCoroutine(AutoDoorR());
		//Star Indicator Light On Coroutine
		StartCoroutine(AutoDoorRindicatorOn());
	}
	
	//Indicator R On Initiator
	IEnumerator AutoDoorRindicatorOn(){
		yield return new WaitForSeconds (2.0f); // wait time
		//initiate Indicator light On
		AutoDoorOpenLightOnRight.SetActive(!AutoDoorOpenLightOnRight.active);
		AutoDoorCloseLightOnRight.SetActive(!AutoDoorCloseLightOnRight.active);
		GetComponent<AudioSource>().PlayOneShot(DTMFtone02);
	}
	
	IEnumerator AutoDoorR(){
		yield return new WaitForSeconds (0.5f); // wait time
		anim.enabled = true;
		//play the Slidein animation
		anim.Play("AutoDoorsIndicatorsSlideInAni");
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideInFX);
	}
	
	//Auto Door Screen Slide Out
	public void  AutoDoorSlideOutR(){
		StartCoroutine(AutoDoorOutR());
	}
	
	IEnumerator AutoDoorOutR(){
		yield return new WaitForSeconds (3.0f); // wait time
		anim.enabled = true;
		//play the SlideOut animation
		GetComponent<AudioSource>().PlayOneShot(AutoDoorSlideOutFX);
		anim.Play("AutoDoorIndicatorsClosed");

	}

}