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.

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


so you would say

VARIABLE.bool = true; 

in your BUTTON.JS script to change it

Hope that helped :slight_smile:

I am copy pasting the answer directly from: BroadcastMessage Functionality Help - Questions & Answers - Unity Discussions

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.