- Home /
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:
- Get all objects tagged as Score Objects
- Check the value of the "myScore" variable of each o those object
- Total up the value of all the "myScore" variables
- Add them to the current "myScoreTotal", making a new "myScoreTotal" value.
Thanks for your time.
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.
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!)
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)
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.
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.