- Home /
Getting values from multiple objects
Hi guys! I've got multiple rigidbodies, which all detect the amount of collision force they recieve. I've created a "Score"-object, which I want to display the added score with just a simple GUI text for now. All the colliding objects have a script with a public float variable "damageReceived". Now, I would like to collect all these "damageReveived" values to my "Score" object. Is it possible to send the values to the "Score"-object, or should the "Score"-object collect them somehow? And of course the score object should add all the values together gathered during the gameplay. Am I approaching this in a completely wrong way? I'm getting quite confused with the GetComponent -stuff here :)
Thanks!
Answer by Cherno · Aug 18, 2014 at 01:32 AM
Assuming you have your rigidbody objects stored in an array or list: Just cycle through them whenever you want to tally your score, and for each one, use something like
using System.Collections.Generic;
public List<GameObject> rigidbodyList;
public void CheckTotalScore() {
foreach(GameObject go in rigidbodyList) {
Score.GetComponent<ScoreScript>().TotalScore += go.GetComponent<ScriptName>().damageReceived;
}
}
Your answer
Follow this Question
Related Questions
Score Counter 2 Answers
Highscore Save 0 Answers
Keeping Score registering more than one Point sometimes on Destroy 2 Answers
GUI Text Score counter 3 Answers
int +=1 won't exceed 1 2 Answers