- Home /
Question by
blue_star1 · May 26, 2013 at 03:08 PM ·
updatemathffindgameobjectswithtagcount
Resource counter doesn't reset on each iteration
The codes is supposed to add the value of addscore(default 10) to the score every count. The addscore however is changed based on the number of gameobjects with a certain tag, therefore changing amount added to the score. but for some reason, when the tagged objects in question are destroyed, the counter doesn't reset. Do you know what I need to change?
using UnityEngine;
using System.Collections;
public class resourceGUI : MonoBehaviour {
private float score = 0;
public float addscore;
private GameObject tax;
// Use this for initialization
void Start () {
InvokeRepeating("EachSecond", 0, 1);
}
// Update is called once per frame
void Update () {
GameObject[] tax;
tax = GameObject.FindGameObjectsWithTag("soldier");
addscore=Mathf.Max(0, 10-tax.Length);
}
void EachSecond(){
score=score+addscore;
}
void OnGUI(){
GUI.Box(new Rect(10,10,100,20), score.ToString());
}
}
Comment
Sorry, by reset I meant that the value of addscore doesn't change to reflect the lower number of tagged objects.
This code worked fine for me. Add this at line 21:
Debug.Log("addscore: "+addscore);
For me, addscore changed with the number of game objects tagged 'soldier' in the game.
Your answer
![](https://koobas.hobune.stream/wayback/20220613102938im_/https://answers.unity.com/themes/thub/images/avi.jpg)