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 TurboHermit · May 25, 2012 at 04:40 PM · arrayvariablesaccessing

Adding variables from all scripts

Hi, I've got a few GameObjects with the same script in my scene. I'm trying to get the values from variables in that script and add them together. I don't really know how to accomplish this.

Any suggestions?

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 whydoidoit · May 25, 2012 at 04:58 PM

Well there are two methods. In the first your script adds a reference to itself to a static list of scripts in OnEnable and removes itself in OnDisable. Then you have an list of all of the scripts so its easy to work through them.

The other method involves using GameObject.FindObjectsOfType(typeof(GameObject)) - (C#) and then working through them getting the component from each if it exists. This method is slow.

Here's method 1 in C#

 using System.Collections.Generic;

 ...

 public static List<YourScriptClass> scripts = new List<YourScriptClass>();

 void OnEnable()
 {
     scripts.Add(this);
 }
 void OnDisable()
 {
     scripts.Remove(this);
 }

 ...
 //In any class
 foreach(var s in YourScriptClass.scripts)
 {
     //Do whatever
 }
Comment
Add comment · 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 TurboHermit · May 26, 2012 at 12:51 PM 0
Share

I don't really understand the first method. I'm in constant need of those variables, it's a number constantly displayed on the screen. What do the Enable and Disable functions do here?

avatar image whydoidoit · May 26, 2012 at 02:42 PM 1
Share

Well OnEnable is called when the game object is activated and disable when it is deactivated or destroyed. $$anonymous$$g. When a new level is loaded or the object is drstroyed

So with method 1 you have an easy way of accessing every instance of your script from anywhere. You also ensure that you are only accessing living objects.

avatar image whydoidoit · May 26, 2012 at 02:50 PM 1
Share

Think of OnEnable like Start but called each time the object is activated. It is obviously activated when it is instantiated or the game starts. After that it will only be called when the object is disabled (using active=false on the game object or enabled on the script) and the reenabled (active=true on game object or enabled on the script)

avatar image whydoidoit · May 26, 2012 at 02:52 PM 1
Share

You could also use Start and OnDestroy if you prefer.

avatar image whydoidoit · May 26, 2012 at 06:43 PM 1
Share

If you mean there is a variable on the script called myVariable, that it is public and you want something else to add them up then, also presu$$anonymous$$g that variable is a float, where you want to add it up you do this:

 float total = 0;
 foreach(var s in YourScriptClass.scripts)
 {
     total += s.myVariable;
 }
Show more comments
avatar image
0

Answer by DarkSlash · May 26, 2012 at 02:12 PM

I found a method that, may be is not the most elegant, but it saves my day:

On the main script I created a variable, lets say "flag" and I set it to false. When I want to change the variables from all objects, I just change the value of flag to true. In the object script, I just add a line to the Update function... if(OtherScript.flag == true) { ... } and that's it.

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 whydoidoit · May 26, 2012 at 02:45 PM 1
Share

Heh if it works for you then fine. This kind of code can prove to be fragile however and hard to debug. I'd prefer a method of directly affecting the other scripts. For instance what sets that flag false and when? Is it possible that it will become false before all of the scripts have updated or perhaps some of the will update many times.

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

6 People are following this question.

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

Related Questions

Accessing Other Script Variables (C#) 2 Answers

Accessing variables from GameObjects in an array 3 Answers

Script access (without script name) 1 Answer

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

[JS] Adding a scripts variable to a variable 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