- Home /
Change variable value of a script from another script. both in different Game object
Hi, I got a script that I want it to show another GUI script after the button is clicked.
Code below is the code that after the button is click to show the another script GUI
var windowRect : Rect = Rect (100,100, 100, 50);
function OnGUI () {
windowRect = GUI.Window (1, windowRect, WindowFunction, "Instruction");
}
function WindowFunction (windowID : int) {
var textFieldString = "text field";
GUI.Label (Rect (25, 15, 300, 30), "Input the keyword to");
GUI.Label (Rect (25, 30, 300, 30), "to search for the book.");
textFieldString = GUI.TextField (Rect (25, 50, 100, 30), textFieldString);
if (GUI.Button (Rect (160, 100, 100, 50), "Search")){
\\This is the location i want to change
\\guiEnable var to true
}
}
========================================================================================= This script is the code that after the value is change the GUI will be shown =========================================================================================
static var guiEnable = false;
function OnGUI(){
if (guiEnable==true){ \\If true then show the GUI below
GUI.Label (Rect (25, 15, 300, 20), "*~Information~*");
GUI.Label (Rect (25, 30, 300, 20), "This is the Newspaper");
if(GUI.Button (Rect (160, 100, 100, 50), "Close")){
guiEnable = false
}
} }
Please anyone can help me with my code? Thanks in advance. Im sorry, that I did not put the code in proper format. I'm still new here and i try use the format but I don't get it to work.
Answer by aldonaletto · Aug 22, 2011 at 07:47 PM
This case is easy, since guiEnable is static: just access it as ScriptName.guiEnable, where ScriptName is the name of the script where guiEnable is defined (without quotes or extension). If the second script is called "GuiScript.js", for instance, use GuiScript.guiEnable:
...
if (GUI.Button (Rect (160, 100, 100, 50), "Search")){
\\This is the location i want to change
GuiScript.guiEnable = true; <- assuming your second script is GuiScript.js
}
} WARNING: Static variables are unique, and are created only once at the beginning of the game - thus, if you had several objects with the second script, all of them will be enabled or disabled at the same time.