Before updating to Windows 10 you may need to return your Pro license. More information here

Push GUI button, change another objects variable


I've created a GUI interface in my game and I have interactive buttons that I've managed to get working. The first thing I did was get objects in the world to change color when I push a button.

Now what I'd like to do is have another objects variable change based on pushing the button.
The script the button is using is BUTTON.JS.

 class Button
     public var object : GameObject
     function OnPush()
         //here's where the code will go to change the variable

Now the code for changing the variable is in VARIABLE.CS

 public bool variable = true;

The thing is that the object that has the variable that needs to be changed isn't a parent, it is a child of a gameobject. Though I'm not sure if that matters considering "var object" in my JS is the correct gameobject.

I'm not sure if I need to use a Broadcast or Send message. Or if it's simply dot operators or what not.



Okay lets try this again...

All the scripts attached to the objects will be in the same folder to get rid of that variable. These two objects are not related to each other at all (parent child wise)

Object one = GUIEditor. I'm dealing with one piece of it, GUI_Button. script below, GUI_Button.JS:

 class GUI_Button extends GUI_Base 
     public var content : GUIContent = GUIContent("");
     public var object: GameObject;
     function OnGUI()
         GUI.skin = GuiSkin;
         if(GUI.Button(getRect(), content))
             if(content.text=="Mini-Map") //if this is the button that was pushed
                  //This is where the code to change the variable goes
                          TESTVARIABLE.TestVar = true;

Okay so that's the script with the button. This next script is the script that has the variable that needs to be changed.

Object two = GUITEST. It is just a cube I"m going to change its color just to make this concept work. The script attached is TESTVARIABLE.CS:

 using UnityEngine;
 using System.Collections;
 public class TESTVARIABLE : MonoBehaviour 
     static bool testVar;
     // Use this for initialization
     void Start () 
         testVar = false;
     static void ChangeVar(bool var)
         if (var)
             testVar = true;
     void Update()
             renderer.material.color = Color.red;

Dominic, feel free to respond to this edit in a new answer so it's easier to follow comments for people in the future. Thanks for the help man, really appreciate it.

more ▼

asked Jul 19, 2011 at 05:43 PM

avatar image

136 98 75 75

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

2 answers: sort voted first

I am copy pasting the answer directly from: http://answers.unity3d.com/questions/145836/broadcastmessage-functionality-help.html

This answer was given by: Alec Slayden

If your game is such that you cannot alter the hierarchy of the objects in question, you can refer directly
to the script on the other object by name, and call the function directly, using GetComponent

for example, instead of BroadcastMessage, you could use:


Where 'OtherScript' is the name of the actual changevar script, minus the extension. If you expect to use
this component again later, it is advised that you store object.GetComponent("OtherScript") in a variable for quick use.

more ▼

answered Jul 21, 2011 at 04:14 PM

avatar image

136 98 75 75

(I shortened the link - Greetz, Ky.)

Jul 21, 2011 at 07:51 PM SisterKy

thanks :)

Jul 21, 2011 at 08:03 PM zachypin
(comments are locked)
10|3000 characters needed characters left

To change variables in different scripts one has to make said variables Static for example

In your VARIABLE.CS script

 change the public bool variable = true;


 `Static bool variable = true;` 

of if you use JavaScript

 Static var bool : boolean = true;

At this point you can change your variable in your BUTTON.JS script by calling it through the other script with the convention of


so you would say

 VARIABLE.bool = true; 

in your BUTTON.JS script to change it

Hope that helped :)

more ▼

answered Jul 19, 2011 at 06:21 PM

avatar image

662 119 115 171

If you have any other problems, let me know :)

Jul 19, 2011 at 06:22 PM AtomicMarine

is there a way to do it with the two scripts not being located in the same folder? the error I get is saying the class is not found, which I get because the class is somewhere else.

Jul 19, 2011 at 06:50 PM zachypin

Check that, it's actually looking directly on the object. At the top of the JS script I have
var Class: Class; "Class" stands for the name of the CS script. I get an error on this line saying "Class does not denote a valid type". I've even created a tester script and moved it within the same folder, not sure why it says this

Jul 19, 2011 at 07:03 PM zachypin

My best suggest here would be to just use an OnGui for the button instead of a class and use that in a standard script attached to an object somewhere in your scene >.<

I have never used static variables with class scripts if you could maybe post both scripts how you did it i could test it and try and help? But yeah probably your best bit is a different button activator or checking how one uses static variables in reference with class scripts

also both should be in the same folder i think >.<

Jul 19, 2011 at 08:12 PM AtomicMarine

okay I'm going to edit my question and put EDIT at the bottom. put the actual scripts in. I toned them down a ton to try and make my question easier to understand.

Jul 19, 2011 at 10:48 PM zachypin
(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



Answers and Comments



asked: Jul 19, 2011 at 05:43 PM

Seen: 3510 times

Last Updated: Jul 21, 2011 at 08:03 PM