x


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.

more ▼

asked May 13 '11 at 10:39 AM

Infinity gravatar image

Infinity
141 36 39 44

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 13 '11 at 11:46 AM

GesterX gravatar image

GesterX
2.1k 13 16 37

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 16 '11 at 07:10 PM

james 6 gravatar image

james 6
132 32 34 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3694
x825
x808
x386
x34

asked: May 13 '11 at 10:39 AM

Seen: 717 times

Last Updated: May 13 '11 at 10:39 AM