• 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
Question by bpears · Jan 24, 2013 at 11:58 PM · javascriptgetcomponent

Shared variable problem, between scripts, help?

So, I am accessing variables in other scripts via unityscript. And since unityscript does not return variables to otherscripts, I have to manually program it. They are fighting eachother for the variables, or if I can get some to update one way, they wont retro update from original script, because the other script is correcting it.

Script 1 should be able to edit both variables (someint1) and (someint2), script 2 should be able to edit 1 variable (invariable1, script 3 should be able to edit 1 variable (intvariable2).

Note that basically, someint1 = invariable1, and someint2 = intvariable2.

The way I have it set up is

Script 1

 var someint1 : int;
 var someint2 : int;
 
 function Start()
 
 someint1 = somenumber;
 someint2 = somenumber;
 
 function Lateupdate()
 
 someint1 = GetObject.Find(GameObject1).GetComponent(GameComponent1).intvariable1;
 someint2 = GetObject.Find(GameObject2).GetComponent(GameComponent2).intvariable2;

Script2

 var intvariable1 : int;
 
 function Update()
 
 intvariable1 = GetObject.Find(GameObject3).GetComponent(GameComponent3).someint1;

Script3

 var intvariable2 : int;
 
 function Update()
 
 intvariable2 = GetObject.Find(GameObject3).GetComponent(GameComponent3).someint2;

Comment

People who like this

0 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 whydoidoit · Jan 25, 2013 at 08:08 AM 0
Share

Glad you got it working!

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by whydoidoit · Jan 25, 2013 at 12:03 AM

Well you are asking it to update all over the place - what are you trying to do? You should have one source of the "truth" about a value and always read and write it there. Also you should not do those Find and GetComponent in an Update call - cache the component reference in Start or Awake (or assign the references to the game objects using the inspector).

Comment
bpears

People who like this

1 Show 7 · 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 25, 2013 at 12:10 AM 0
Share

The variables get edited in different functions in each script, so So I can't have one "truth". And if I put them @ function Start, then they do not update when edited. I need to make it so they can all do this without interfering with each other. How can I let them be edited in different scripts?

avatar image whydoidoit · Jan 25, 2013 at 12:13 AM 1
Share
  GameComponent3 gc3;

 function Start()
 {
     gc3 = GameObject.Find("GameObject3").GetComponent(GameComponent3);
 }

  function Update() 
  {
     gc3.someint2 += 20;
  }
avatar image bpears · Jan 25, 2013 at 12:18 AM 0
Share

if I do not have the other scripts access the variables, basically getting them back, then the scripts end up with different ideas of what the variables should be. When editing in one script, the value doesnt return to original script, so that is why I do it that way. C# will return the variable, but not unityscript.

@whydoidoit Or are you meaning something else? I don't quite get what your saying with that code. GameComponent3 gc3; but then you have GameComponent3 and gc3 separated..?

avatar image bpears · Jan 25, 2013 at 12:22 AM 0
Share

maybe a sendmessage approach would be more appropriate? Send message, telling script2 & script3 to perform function to edit the variable, instead of editing/accessing variable in script1.

I would like to try to keep it how I have. But it to work all the way. So if theres a way...

avatar image bpears · Jan 25, 2013 at 01:23 AM 0
Share

@whydoidoit

I tried to test your way, but I get error: cannot convert 'gamecomponent' to int.

Edit: just had to change the vars from var asdf : int; to var asdf;. But now it is saying unknown identifier for the variables I am trying to access.

Edit2: Nevermind I had a typo... testing now.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Multiple Cars not working 1 Answer

Get object, which is more optimized? 1 Answer

How to access .int variable? 1 Answer

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

Comparing variable from another script, help? 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