- Home /
Push GUI button, change another objects variable
Hey,
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.
Thanks.
EDIT:
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;
static var TESTVARIABLE : TESTVARIABLE;
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()
{
if(testVar)
{
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.
Answer by zachypin · Jul 21, 2011 at 04:14 PM
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:
object.GetComponent("OtherScript").ChangeVar(true);
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.
Answer by AtomicMarine · Jul 19, 2011 at 06:21 PM
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;
to
`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
scriptname.variablename;
so you would say
VARIABLE.bool = true;
in your BUTTON.JS script to change it
Hope that helped :)
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.
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
$$anonymous$$y best suggest here would be to just use an OnGui for the button ins$$anonymous$$d 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 >.<
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.
Your answer
Follow this Question
Related Questions
Expecting variable error with a button 1 Answer
Adding an extra variable to GUI buttons? 2 Answers
Is there any way to move a GUI.Button across the screen (when clicked) with a script? 1 Answer
GUI Button sound problem, don´t work 1 Answer
GUI.button Texture shows up in Unity but doesn't show on android device. 1 Answer