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.
public var object : GameObject
//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;
static var TESTVARIABLE : TESTVARIABLE;
GUI.skin = GuiSkin;
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:
public class TESTVARIABLE : MonoBehaviour
static bool testVar;
// Use this for initialization
void Start ()
testVar = false;
static void ChangeVar(bool var)
testVar = true;
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:
Where 'OtherScript' is the name of the actual changevar script, minus the extension. If you expect to usethis component again later, it is advised that you store object.GetComponent("OtherScript") in a variable for quick use.
(I shortened the link - Greetz, Ky.)
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;
`Static bool variable = true;`
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 :)
If you have any other problems, let me know :)
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
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 >.<
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
5 People are following this question.
Expecting variable error with a button
Is there any way to move a GUI.Button across the screen (when clicked) with a script?
Adding an extra variable to GUI buttons?
How can I get the Unity 3 texture for 'button on'?