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.

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.