How to sum up values from clones
Me and my friends are making a game where your supposed to cut bushes. I have a bush prefab. The bushes are randomly spawned in different positions and when it spawns the scale of the bush gets bigger. We wonder if it's possible that we can sum up the scale from all the different clones spawned into 1 variable. When the sum of scale is at a specific number you've basically lost.
Answer by Craftist · Jan 29, 2020 at 02:36 PM
You can create a class with a static variable, and when the bush grows in the bush component, you simply add the same value you add to a local bush scale to a global variable. Like this:
class Data { public static float GlobalScale = 0.0f; }
class Bush : MonoBehaviour {
void Update() {
transform.scale += 0.5f*Time.deltaTime; //I know this is wrong but this is for the sake of example
Data.GlobalScale += 0.5f * Time.deltaTime;
}
}
And then since the Bush.Update method is called on every bush item, it will accumulate the growth in the global Data.GlobalScale field. Is this what you needed?
Your answer
![](https://koobas.hobune.stream/wayback/20220612222327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My health bars image.fillAmount doesnt change. 4 Answers
Health pickup script issue -1 Answers
Adding variable to object 1 Answer
How to make GUI variables compatible with the UI system? 0 Answers
Health variable not changing 1 Answer