- Home /
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
Answer by Max Kaufmann · Dec 13, 2010 at 03:17 AM
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 } }
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?
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can one use of a static variable, change it globaly? 2 Answers
Showing GUI by pressing diffrent buttons? 1 Answer
Difficulty Calling Variables from other scrtipts 1 Answer
How can I set the order of destruction of game objects? 1 Answer