|
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.
(comments are locked)
|
|
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:
Then the script in Scene 2 will be something like this:
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:
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)?
Dec 02 '10 at 10:05 PM
Tyler 2
So you're saying you'd like the player to be able to save several configurations of every option (Like difficulty = easy; Mode = 1; etc etc) so they can come back and use that same configuration later on?
Dec 02 '10 at 11:20 PM
Kristov
Yeah...is that possible?
Dec 03 '10 at 05:31 AM
Tyler 2
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..
Dec 03 '10 at 08:33 AM
Kristov
... 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!
Dec 03 '10 at 08:44 AM
Kristov
(comments are locked)
|
|
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
SceneManager script
Should be enough to get you started working with this way of doing it.
(comments are locked)
|
