- Home /
Toggle button ?
Hello. I know in unity you can use toggle buttons. I want to affect which scripts are active in another scene, how can I make it so that when "Toggle Button A" is checked script A will be active when another scene loads? To put that in context, my game will (hopefully) have a wide variaty of options(difficulty, stage layout, mode, player type, etc). Rather than make a separate scene for each possible combination, I want to make each individual option its own script and then just activate it depending on the checked buttons. I hope that makes sense, Thanks.
I am amazed by the discussion here. I also have some experience on the C# toggle button control. I found the codes here are quite clean and practical to resolve this situation. And sounds like the UI button controls are also good at doing this. I'd like to take a try.
Answer by Kristov · Dec 02, 2010 at 06:01 AM
Another way to do it is with PlayerPrefs. In the first scene, you could use PlayerPrefs.SetInt to store whether a script in the next scene will be active or not (so the int will be either 0 or 1), and in the second scene use PlayerPrefs.GetInt to find the value:
Scene 1's script will be something like this:
var toggleBool = true;
function Update () { if (toggleBool == true && PlayerPrefs.GetInt("toggleExample") == 0) { PlayerPrefs.SetInt("toggleExample", 1); } else if (toggleBool == false && PlayerPrefs.GetInt("toggleExample") == 1) { PlayerPrefs.SetInt("toggleExample", 0); } print (PlayerPrefs.GetInt("toggleExample")); }
function OnGUI () { toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Toggle Example"); }
Then the script in Scene 2 will be something like this:
function Start()
{
if(PlayerPrefs.GetInt("toggleExample") == 1)
{
GetComponent(otherScript).enabled = true;
}
else
{
GetComponent(otherScript).enabled = false;
}
}
in terms of the GetComponent(otherScript), the (otherScript) part will be the name of your script controlling the difficulty, and (if you want to use GetComponent) will be attached to the same Game Object as this script.
A good reason to use PlayerPrefs is that you can change the integer to something that's not 0 or 1, so if instead of a single toggle (which'd only be on or off) you can have a GUI toolbar (Easy/Medium/Hard). The scene 2 script would then be like this:
function Start() { if(PlayerPrefs.GetInt("difficulty") ==1) { GetComponent(difficulty1).enabled = true; GetComponent(difficulty2).enabled = false; GetComponent(difficulty3).enabled = false; }
if(PlayerPrefs.GetInt("difficulty") ==2)
{
GetComponent(difficulty1).enabled = false;
GetComponent(difficulty2).enabled = true;
GetComponent(difficulty3).enabled = false;
}
etc...
}
Another (possibly) good reason to use PlayerPrefs is that it persists even when you close the program, so when the player picks their preferences, they won't have to re-do it every time they load the game. Although if you don't want that, you could make a script that resets the preferences at startup.
Hope that helped!
Is there a way I can record what combination is selected and save it in player prefs (so I can then display it in a "Favorites" type menu)?
So you're saying you'd like the player to be able to save several configurations of every option (Like difficulty = easy; $$anonymous$$ode = 1; etc etc) so they can come back and use that same configuration later on?
Yeah it'd definitely be possible, if a little long winded. If you made a menu for setting/loading favourite configs, you'd need to make 2 buttons for each config; so for config 1, it'd be "Set Config 1" and "Load Config 1" for example. To save each config so that the player can re-access it after they quit the application and come back into it again, you'd need to use PlayerPrefs (handy, aren't they?). You'd need to make a PlayerPrefs.SetInt for EACH option in EACH config; so difficultyConfig1, modeConfig1, difficultyConfig2, modeConfig2 etc etc. When you SAVE the current option setup into a..
... config, you'd want to get each option's current value with PlayerPrefs.GetInt("option") and then store it in that config's option value of the same kind. So the line would be (if using config 1) PlayerPrefs.SetInt("optionConfig1", PlayerPrefs.GetInt("option"); (Remember when you use GetInt it comes back with the value) and you'd need to do that for each option. To LOAD a particular config, you'd need to do the opposite: PlayerPrefs.SetInt("option", PlayerPrefs.GetInt("optionConfig1"); I hope that makes sense!
Answer by Rennat · Dec 02, 2010 at 05:46 AM
You'll need to store the checkbox values on a game object that persists across scene changes then when the scene is loaded enable and disable scripts based on the values.
example:
SettingsManager script
/* this script would be on the persistent object that is in the menu scene but not the gameplay scene, it will persist into the gameplay scene. In this example this game object is named "Settings Manager" at the root level of the menu scene */
var hardMode : Boolean = true; var fastMode : Boolean = false;
function Start () { DontDestroyOnLoad(gameObject); }
SceneManager script
/* For this example the HardModeScript is on this same game object and FastModeScript is on a different game object. */
var fastModeGameObject : GameObject; // set in the inspector
private var hardModeScript : HardModeScript; private var fastModeScript : FastModeScript;
function Start () { hardModeScript = gameObject.GetComponent(HardModeScript); fastModeScript = fastModeGameObject.GetComponent(FastModeScript);
// find the settings manager
var settingsObject : GameObject;
var settingsManager : SettingsManager;
settingsObject = GameObject.Find("/Settings Manager");
if (!settingsObject) { Debug.Log("Didnt find settings manager"); return; }
settingsManager = settingsObject.GetComponent(SettingsManager);
// enable/disable scripts
hardModeScript.enabled = settingsManager.hardMode;
fastModeScript.enabled = settingsManager.fastMode;
}
Should be enough to get you started working with this way of doing it.