|
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).
So basically, I would like to:
Thanks for your time.
(comments are locked)
|
|
You are very close. You just want to iterate through the array and add each value to your running total.
Let me also suggest a change that would not require you to Get the component every 2 seconds and will give you better performance.
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!)
Dec 22 '10 at 10:35 PM
Archangel3d
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' instead? etc)
Dec 23 '10 at 05:34 AM
Archangel3d
I edited my post.
Dec 23 '10 at 01:43 PM
Peter G
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.
Dec 23 '10 at 03:20 PM
Archangel3d
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 SendMessage() that way you only need to recreate it when it changes.
Dec 23 '10 at 06:05 PM
Peter G
(comments are locked)
|
