- Home /
Remember scene settings when returning to scene
I'm not sure what to call this function or game mechanic. But lets say i have a scene where UI buttons control things like animation and sound on a UI interface GUI and I press a pause animation and sound button but yet when I exit that scene and later come back to it my music and animation will be playing by default and NOT pause from my last visit. Hopefully I'm explaining this right.?
Is there a way in C# to remember such setting across scene changes? Perhaps with some kind of manager or something?
Could use playerprefs, if your scene is set up in a way which works with this.
You must appreciate what is involved when it comes to saving a scene, mid-progress. You pretty much need to record everything and reproduce it when required.
Inspector child object go missing when I return to main scene?
Answer by wesleywh · Jan 23, 2015 at 04:56 PM
So you will need a GameObject that won't be destroyed between scenes. So you can attach a script to that object that can store variables between scenes. You will need to attach a DontDestroyOnLoad code.
Here is Unitys API on it: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
In that objects script you can check it for some play or pause bool.
Is there a good tutorial anywhere on how to implement all of this stuff?
It's very simple, whatever script you store all your settings/prefs on add the below code and it will now survive the transition from one scene to another. This means whatever variables that have changed on this script will stay changed until you quit the game.
void Awake ()
{
DontDestroyOnLoad (transform.gameObject);
}
If you want the data to survive even after you've quit the game so it persists the next time you play, you'll need to use PlayerPrefs that I linked above, and don't be daunted it's pretty easy to use too. For tutorials just search on youtube
Edit - Just note, the gameObject this script it on wil survive the transition, and all of it's components, including other scripts. So it might be best to create a gameobject specificaly for this script. And make sure it doesn't already exist in the scene you're going to to avoid duplicates
O$$anonymous$$ I tried doing sort of what you have here like this: using UnityEngine; using System.Collections;
public class UI$$anonymous$$anagerScript : $$anonymous$$onoBehaviour {
void Awake ()
{
DontDestroyOnLoad (transform.gameObject);
}
public Animator LEDlights;
public void PlayLEDLightsAnimation()
{
LEDlights.SetBool("isPlaying", !LEDlights.GetBool("isPlaying") );
}
}
Now this is on an empty game object that the UI button calls to, but when I pause the animation and exit the scene and come back in my animation is playing so something is missing here?
The bool that tells the game whether or not it is paused needs to live on this UI$$anonymous$$anagerScript and any other script needs to be checking this bool on Start or Awake, or maybe even Update
Thanks Fourthings, yup this seems a little more complicated than I thought, I'll get my code guy to have a look at it when he gets back from his trip in a week, figured while he was away i would try and get some of the nit picky stuff done, did manage to get some stuff done so that saves him time there but this seems more something for him to look into, without a more detailed description of what goes where I'm kinda lost. Anyways thanks for the help it al least points us in the right direction and I know he will know exactly what you are talking about ;)