x


Defining a variable in another script?

Hello. These are the scripts for selecting the options for my game (mode, difficulty, etc.). How it works is that the menu are a bunch of toolbars. Each toolbar option selected represents a number, that number represents a number in a different script, and that other script contains the functions that start the options (like "Standard difficulty=true")

If I define something to happen in the script that has the toolbat GUI like this, the other variable (in this case, "Score") works, Its changed to whatever is defined.

var ModeInt = 0;
var ModeStrings : String[] = [];    

function OnGUI() 
{
    ModeInt = GUI.Toolbar (Rect (240, 270, 500, 50), ModeInt, ModeStrings);
}


function Update (){
if (ModeInt==2) {Stats.Score=1000}

}

I then tried to make a variable in the GUI script affect a variable in another script (in this case the "ArcadeOptions" script) :

if (ModeInt==0) {ArcadeOptions.Mode =2;}

(Here is the ArcadeOptions Script)

static var Mode = 5;

function Update (){
if (Mode ==0) {Stats.Score=1000;}

}

and it works, the Score value is changed.

The problem happens when I try to have the "ModeInt==" something other than 0(so that other buttons can affect the "ArcadeOptions" script). No matter what the element of that toolbar is, if it is not 0, nothing happens in the other script, what is going on(I am getting no errors from unity)?

Thanks

more ▼

asked Dec 13 '10 at 02:11 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

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

1 answer: sort voted first

You need to create a reference to the instance of the first script.

var scriptWithMode : ScriptWithModeInt; // drag-and-drop from inspector

function Update() {
  if (scriptWithMode.ModeInt == 0) {
    // foo
  }
}
more ▼

answered Dec 13 '10 at 03:17 AM

Max Kaufmann gravatar image

Max Kaufmann
573 10 12 21

Do I put that in the script with the GUI or in the "ArcadeOptions" script? Am I writing the script backwards compared to yours (I have the toolbar affect the variable in the "ArcadeOptions" Script, are you having the "ArcadeOptions" script read the changes in the GUI script?

Dec 13 '10 at 03:38 AM Tyler 2
(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:

x5062
x385
x341
x27

asked: Dec 13 '10 at 02:11 AM

Seen: 905 times

Last Updated: Dec 13 '10 at 02:11 AM