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
1
Question by Archangel3d · Dec 22, 2010 at 10:12 PM · arrayvariablemultiple

Getting variables of objects in an array, then adding them together

This may be a case of not having the right grammar or mindset, but I seem to have hit a wall here.

Explanation: I have a number of objects in the scene tagged as "ScoreObject", and each has a "CountScore" script that constantly increases the object's "myScore" variable.

I'm trying to calculate the total score accrued. Every 2 seconds, the score tracker script should check each object named "Score Object", get the value of all of their myScore variables, add them together, then add them to the scripts own "myScoreTotal". The number of ScoreObjects can vary in the game.

Now, I'm still learning, and the creation and accessing of Arrays is still a little foreign to me, so I ask for your patience and understanding (i.e. talk to me as if I was dumb.) I've got this so far, and I'm not even sure if this is good. I especially don't know how to get the variables contained within each object in the array (hence the title).

function Start() 
{
    var timeDelay = 2;
    var myScoreTotal = 0;
    while (true) 
    {
    yield WaitForSeconds(timeDelay);
    var scoreObjects = GameObject.FindGameObjectsWithTag ("ScoreObject");
        for (var scoreobject in scoreObjects)
        {
            //do something I have no idea anymore augh
        }
    }
}

So basically, I would like to:

  1. Get all objects tagged as Score Objects
  2. Check the value of the "myScore" variable of each o those object
  3. Total up the value of all the "myScore" variables
  4. Add them to the current "myScoreTotal", making a new "myScoreTotal" value.

Thanks for your time.

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

1 Reply

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

Answer by Peter G · Dec 22, 2010 at 10:22 PM

You are very close. You just want to iterate through the array and add each value to your running total.

while (true) {

 yield WaitForSeconds(timeDelay);
 var totalScore = 0;
 //The scope of the variable doesn't make much difference.
 //If you put it here, then your fine.
 //If you put it where you had it then you should reset it here.

 var scoreObjects = GameObject.FindGameObjectsWithTag ("ScoreObject");
 for (var scoreobject in scoreObjects)
 {
     //Here's the line you want.
     totalScore += scoreobject.GetComponent(ScoreScriptName).score;
     //The variable score is not part of the GameObject class, but it is part
     //of your script.
 }

 //Do something with the score here.

}

Let me also suggest a change that would not require you to Get the component every 2 seconds and will give you better performance.

var scoreObjects : YourScriptName[] = FindObjectsOfType(YourScriptName); //This will give you all the Score keeping components in an array. //And since you already found them, you will not have to waste processing power //every 2 seconds to find them again.

while (true) {

 yield WaitForSeconds(timeDelay);
 var totalScore = 0;
 //The scope of the variable doesn't make much difference.
 //If you put it here, then your fine.
 //If you put it where you had it then you should reset it here.

 for (var scoreobject in scoreObjects)
 {
     //Here's the line you want.
     totalScore += scoreobject.score;
     //Now since you already have the scoreObjects you do not need to use
     //GetComponent.
 }

 //Do something with the score here.

}

Unity has an Object-Oriented programing style so you have to make sure that if you try and access a variable that is not part of your script, that you first find the object it is attached to in this case the ScoreObject component. That's another thing. Objects do not have to be GameObjects in fact most of them aren't.

Comment
Add comment · Show 5 · 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 Archangel3d · Dec 22, 2010 at 10:35 PM 0
Share

Awesome. Thank you so much. (and such a timely response too!) I'm glad to know I wasn't too far off, it gives me a lot more confidence that I'm actually learning (gasp!)

avatar image Archangel3d · Dec 23, 2010 at 05:34 AM 0
Share

Actually I seem to be running into a side issue: it's telling me that "score" is not a member of UnityEngine.GameObject. I'm think I know how to access an individual object's variables/scripts, but I'm just checking back to ask if I missed anything (for example,: the "score" variable is a 'var', should it be a 'public var' ins$$anonymous$$d? etc)

avatar image Peter G · Dec 23, 2010 at 01:43 PM 0
Share

I edited my post.

avatar image Archangel3d · Dec 23, 2010 at 03:20 PM 0
Share

Aaaaah, I see. I need to find a tutorial about the whole Array and "[]" usage. Side question (sorry I'm pestering you like this), but if I add a new scoreObject later (through instantiation), would that top FindObjectsOfType pick the new one up? That's why I had originally made the var check every 2 seconds, in the event a new scoreObject was created.

avatar image Peter G · Dec 23, 2010 at 06:05 PM 1
Share

You are correct, it will not pick up the new one. You might want to set up a system that rebuilds the array whenever you add a new one using Send$$anonymous$$essage() that way you only need to recreate it when it changes.

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

No one has followed this question yet.

Related Questions

How can I access the variables of arrays with multiple data types? 1 Answer

How to make an array of Interactive Clothes in one variable 0 Answers

Store Multiple values in one variable 2 Answers

Playing multiple sounds on same key input ("e") 1 Answer

Command all array variable values 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