Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by BZS · Jan 05, 2014 at 02:51 AM · javascriptclick objectsanother script

Accessing variable in another script?

I am trying to make it so when I click a gui button and a variable in another script is true it will change the color of an object. Here is my code:

 //Real is the other script
 var test : Real;
 var testObject : GameObject;
 function OnGUI () {
 test = gameObject.GetComponent("Real");
     if (GUI.Button (Rect (10,10,100,75), "I am a button")) {
           //selected1 is the variable in the other script I am trying to access
         if(test.selected1 == true){
         testObject.renderer.material.color = Color.yellow;
         }
     }
     }
 
Comment
Add comment · Show 1
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 Tomer-Barkan · Jan 05, 2014 at 11:43 AM 0
Share

There are hundreds of answers to this questions already. https://www.google.com/search?q=Accessing+variable+in+another+script&oq=Accessing+variable+in+another+script&aqs=chrome..69i57j69i60j0l3.187j0j1&sourceid=chrome&ie=UTF-8

2 Replies

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

Answer by iwaldrop · Jan 05, 2014 at 04:29 AM

Your script has a bunch of unnecessary lines of code in it. Additionally, 'Real' is an awful Object name. Classes define objects, and objects are things, so their names should always be nouns, whereas the word Real is an adjective. Your script names should be as descriptive as possible so that others reading your code know what they mean. I once ran across a script named 'TTTTTT'...no kidding. What in the world is that?

Anyway, back to your question. First off, instead of dragging the GameObject into the GameObject variable, and then getting the Real component at runtime (and at least twice per frame when done in OnGUI), you should just drag the GameObject into the Real field. Unity is smart enough to link up the script instead of the GameObject. If you must get it at runtime, you should use a separate method to do that work for you.

Finally, the same goes for variable names as does class names; they should be meaningful. The variable selected1 doesn't tell us anything about what that variable stores, and only through seeing how you've used it can we tell what type it is. Boolean values should, in good coding practice, always be named is{Something}, like isActive, isBlue, isMoving, etc. This tells anyone reading your code that it is a boolean value, and when it is true it 'is active'. Unity doesn't do this, so if you're when in Rome type, then follow their lead. But be aware that there are coding conventions for each language, and if you don't adhere to them then expect to get an earful from colleagues at some point.

Example using Real

 var real : Real;
     
 function OnGUI ()
 {
     if (GUI.Button(Rect (10,10,100,75), "I am a button") && real.selected1)
         testObject.renderer.material.color = Color.yellow;
 }

Example of Method Returning a Script

 private var real : Real;
 
 function GetReal(go : GameObject) : Real
 {
     if (real == null)
         real = go.GetComponent(Real);
     return real;
 }

Example using GameObject

 private var targetObject : GameObject;
     
 function OnGUI ()
 {
     if (GUI.Button(Rect (10,10,100,75), "I am a button") && GetReal(testObject).selected1)
         testObject.renderer.material.color = Color.yellow;
 }
 
 function GetReal(go : GameObject) : Real
 {
     if (real == null)
         real = go.GetComponent(Real);
     return real;
 }

I went off on a long rant about coding practices because, well, this question is literally asked at least once a week here. For some reason it keeps getting asked, even though it is answered at least once a week as well. I'm not sure what's so difficult about performing a google search. It is a lot faster than asking a question on a board and waiting for someone to come around and dilly dally around by critiquing your code and giving you a lot more information than you were probably looking for.

Check it out!

Comment
Add comment · 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
1

Answer by Aresak · Jan 05, 2014 at 01:10 PM

There is nice answers here and here

Comment
Add comment · 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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to detect clicks from other objects in one script? 2 Answers

How to make a selection system? 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