Level point Total?

I am looking for recommendations on a quick Javascript function that can help me summarize the points total for a level. I can add points from an item to the players total easy enough and other bits, the point I'm stuck on is summing the potential level total.

I have multiple objects in the scene with point values that vary from item to item. Can I access this through a single variable or do I need to assign the variable by item and then hardcode in the "object one points" + "object two points", blah, blah blah. It would certainly seem the former given a static variable add function, but it alludes me.

Adding for clarity: To that, it would seem Objects with point values might have a script attached to them with:

static var pointsAre : int = XXX;

Then my in my SceneManager/GameState JS:

private var totalPoints : int;

function Awake () {
    totalPoints = pointsAre + pointsAre...;  //ACK - but how do I simply sum their accumulated total from all Objects in the scene.

Can this be done with a single static variable call?


You could create a JS file called "GameState" and define a static variable:

static var Score : int = 0;

You do not have to assign this file to a game object, just store it in your assets folder. Then you have access to this variable by other scripts like this:

GameState.Score += ...

One thing you could do is make an empty game object with a script on it that, say, during Start() you run through your level to detect all of those objects (Tag them all the same).


var totalPoints : int;
var allScoreItems = GameObject.FindGameObjectsWithTag ("ScoreItem");
for (var oneItem in allScoreItems)
    totalPoints += oneItem.pointsAre;

I apologize if my Java syntax is a little hazy, I work in C#/C++ mostly.