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 bpears · Jan 23, 2013 at 02:03 AM · javascriptvariablegetcomponent

When editing a scripts variable in another script, does the original script update?

If you pull a number variable from a script, and in another script, say subtract amount from variable, will that number be updated in the original script?

To go in more depth, I have a int variable from script1. I am subtracting from that variable in script2. So, how do I make script1 updated to the new int? The variable from script1 is defined as a new variable in script2 if that matters.

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
1
Best Answer

Answer by Owen-Reynolds · Jan 23, 2013 at 03:22 AM

If you want to change a variable in another script, just change it: cowScript.x = cowScript.x + 2;

If you copy it, then changing the copy won't do anything to the original. But, a common trick to save typing is to copy, change and copy back:

 x2 = cowScript.x;
 if(x2<5) .... x2+=6; if(x2>10) .... // don't have to write cowScript.x so much
 cowScript.x = x2; // copy back when done

But, some variables are actually just links (pointers.) For example, transforms. If you copy a transform, you don't really. You just get another link to the original transform (yes, this is a very confusing rule.) So this works:

 var tt : Transform = cowScript.tail;
 tt.localPosition = Vector3.zero;
 // same as cowScript.tail.localPosition = Vector3.zero;
Comment
Add comment · Show 4 · 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 bpears · Jan 23, 2013 at 03:53 AM 0
Share

OH$$anonymous$$EHGERD! Not what I did really, but you ignited some logic in me, and it works now! woo! thanks!

avatar image Dejan1 · May 09, 2013 at 09:50 PM 0
Share

Cannot update original variable from script1 either.I can read the variable with getcomponent function in script2, but can not change the original value(from script1). C# - unity answer please. I need to change int variable.

avatar image Dejan1 · May 09, 2013 at 11:07 PM 0
Share

Found it.If one makes a variable in script1 public static (public static int myglobalvariable - for instance),they dont even have to use GetComponent function to read it in script2.Just type script1name.myglobalvariable=somenumber;(to change it globaly(for entire game)) in script2.

avatar image Owen-Reynolds · May 10, 2013 at 01:28 AM 0
Share

Statics usually end up causing more trouble than they save. They often need to be ripped out later and redone as a regular variables.

The whole process for normal vars is: make a var to link to the script: public otherScript SS;. Then make SS actually link. Either drag in the object, or SS = GameObect.Find("abc").GetComponent("otherScript"); Then use SS: SS.x = 8;.

avatar image
1

Answer by Catlard · Jan 23, 2013 at 02:52 AM

Hey there, bpears. There are two ways to get a variable from another location -- by reference and by value. When you take a variable from a script1 normally (by reference), without using the "ref" keyword, you get a copy that has no effect on the original variable. But if you use the ref keyword, the variable's location in memory will be passed as well -- so the original value of the int variable in script1 can be changed in script2.

This should explain the syntax: http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

Comment
Add comment · Show 1 · 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 bpears · Jan 23, 2013 at 02:59 AM 0
Share

I see, but I am using unityscript, and a search led me to believe there is no such thing in unityscript. So somehow I need to return the value back or something similar... hmmm... thanks though!

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

13 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

Related Questions

GetComponent from class 1 Answer

Issues accessing a variable in another script 0 Answers

Passing a Script Name to a Function 2 Answers

Assigning a unity component variable issue 1 Answer

AddComponent with parameter variable 2 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