Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by zachypin · Jul 19, 2011 at 05:43 PM · guibuttonvariablemessageoop

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Jul 21, 2011 at 07:51 PM 0
Share

(I shortened the link - Greetz, $$anonymous$$y.)

avatar image zachypin · Jul 21, 2011 at 08:03 PM 0
Share

thanks :)

avatar image
0

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 :)

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AtomicMarine · Jul 19, 2011 at 06:22 PM 0
Share

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

avatar image zachypin · Jul 19, 2011 at 06:50 PM 0
Share

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.

avatar image zachypin · Jul 19, 2011 at 07:03 PM 0
Share

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

avatar image AtomicMarine · Jul 19, 2011 at 08:12 PM 0
Share

$$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 >.<

avatar image zachypin · Jul 19, 2011 at 10:48 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges