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 /
This question was closed Jul 20, 2013 at 04:54 AM by Eric5h5 for the following reason:

Duplicate Question

avatar image
0
Question by essell · Jul 20, 2013 at 04:27 AM · variablesaccessing

How do I change a variable in Script A from inside Script B

I have a scriptA as a component of gameObjectA, and scriptB as a component of gameObject B.

I have some variables declared and used in scriptA, and I'd like to be able to change them from scriptB.

What are the simplest methods that people would suggest?

P.S. I'm using Javascript, and thanks in advance.

Comment
Add comment · Show 2
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 Eric5h5 · Jul 20, 2013 at 04:55 AM 0
Share

Please search first; this is the most-asked question and must have been answered literally hundreds of times by now.

avatar image essell · Jul 20, 2013 at 04:56 AM 0
Share

I promise I did, but the examples I found didn't explain it in a way that helped me solve my problem, I'm afraid.

1 Reply

  • Sort: 
avatar image
0

Answer by aldonaletto · Jul 20, 2013 at 04:50 AM

Scripts in Unity are components (like Renderer, CharacterController, Collider etc.), thus you must get a script with GetComponent(ScriptName). If scriptA and scriptB are both attached to the same object, get a reference to scriptA with GetComponent:

 // assuming that ScriptA is the name of scriptA:
 var codeA: ScriptA = GetComponent(ScriptA);
 codeA.variable1 += 1; // modify the variables
 codeA.variable2 = false;

If scriptA is attached to a different object, you must have a reference to this object:

 // otherObject is a reference to the other object:
 var codeA: ScriptA = otherObject.GetComponent(ScriptA);
 codeA.variable1 += 1; // modify the variables
 codeA.variable2 = false;

Any reference to the game object or any of its components works (transform, collider, renderer, other script references etc.)

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 essell · Jul 20, 2013 at 05:32 AM 0
Share

Hmm... what does codeA refer to? And I assume the first line goes before functions with other variable declarations, whereas the other two lines would go in update?

avatar image aldonaletto · Jul 20, 2013 at 07:30 PM 0
Share

The code above should be part of the function that modifies ScriptA variables. If you're modifying ScriptA in the Update function, place the whole code inside it. If ScriptA is always the same, place the variable codeA outside any function and initialize it at Start, and modify its variables wherever you want - like this:

 private var codeA: ScriptA;
 
 function Start(){
   codeA = GameObject.Find("ObjectA").GetComponent(ScriptA);
 }
 
 function Update(){
   codeA.variable1 += 1;
 }

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Accessing Other Script Variables (C#) 2 Answers

Adding variables from all scripts 2 Answers

Error Accessing Other Script's Variables 1 Answer

How can I edit my static variable in the editor? 1 Answer

Get script variable from collider 3 Answers


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