- Home /
Passing a variable through the main menu
Hi, actually in my main menu I have one option only, one called "Play", I want now to implement a couple of them "Single player play" and "Two players play". On my main Scene called "TheGame" I have both players, but I want that if I someone clicks on Single Player, the object of my second player disapears. Anyone knows what I have to do? Here I have attached the script of the button "Play" on my Main menu
if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height -160, 140, 70), "Play"))
{
isLoading = true;
Application.LoadLevel("TheGame");
}
Hope you can help me, thanks.
Answer by GesterX · May 13, 2011 at 11:46 AM
You could have a "GameManager" object which holds a variable for the number of players. Just be sure to have this in a script attached to it:
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
This way the object won't get destroyed between scenes.
When the player clicks the Single Player button just set the number of players variable to 2. From there when you load the next scene you can check the number of players (probably in your Awake or Start function) and do whatever you need to with destroying/creating players.
Answer by james 6 · May 16, 2011 at 07:10 PM
the best way to do it is using PlayerPrefs
read the link here
code snippet:
PlayerPrefs.SetInt("player",1);
loading it
if(PlayerPrefs.GetInt("player")==1) ....
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html