Space Shooter: UpdateScore method doesn't work properly.

Hey there.
Im finishing hte tutorial but now I found this problem. The UpdateScore() method is only called at the very first frame. I triend placing a call to this method into a Update() method and it worked perfectly, so I asume the problem is not ON the UpdateScore() method, but IS the method itself.
Sorry if I mispelled something, I´m Spanish and my English is not all it could be.

Here is the code:

public class gameController : MonoBehaviour {


    public GameObject hazard;
    private Vector3 hazardPosition;
    public int hazardCount;
    public float startWait;
    public float spawnWait;
    public float waveWait;
    private int score;
    public GameObject scoreText;
    void Start ()
    {
        SpawnWaves();
        StartCoroutine (SpawnWaves());
        score = 0;
        UpdateScore();
    }


    IEnumerator SpawnWaves()
    {
        while (true)
        {
            yield return new WaitForSeconds(startWait);
            for (int i = 0; i <= hazardCount; i++)
            {
                hazardPosition = new Vector3 (Random.Range(-5, 5), Random.Range (-5, 5), 20);
                Instantiate(hazard, hazardPosition, Quaternion.identity);
                yield return new WaitForSeconds(spawnWait);
            }
            yield return new WaitForSeconds(waveWait);
        }
    }
    public void adScore(int scoreValue)
    {
        score = score +  scoreValue;
    }
    void UpdateScore ()
    {
        scoreText.GetComponent<GUIText>().text = "score: " + score;
    }
    
  
}

And:

public class arteroid : MonoBehaviour
{
    public float rotacion;
    private Rigidbody rb;
    public GameObject explosion;
    public float speed;
    private gameController gameController;
    public int scoreValue;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.angularVelocity = Random.insideUnitSphere * rotacion;
        rb.velocity = transform.forward * (-speed);
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<gameController>();
        }
        else { Debug.Log("se ha encontrado el script 'gameController'"); }   

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "capsule") { return; }
        DestroyObject(other.gameObject);
        DestroyObject(gameObject);
        Instantiate(explosion, rb.transform.position, rb.transform.rotation);
        gameController.adScore(scoreValue);
        

    }



}

Thanks a lot for whoever replies me :slight_smile:

Of course it will be called only once, because you are calling it from Start() , which is called the first time script is loaded, and not from anywhere else.

Your UpdateScore() function contains a call to GetComponent and is also performing a string concatenation, therefore, putting it inside Update() is a bad idea, because you’ll be calling it every frame, and thus it will be expensive.

What you should do is, call UpdateScore() function whenever there is a change in the score variable, like this :

public void adScore(int scoreValue)
     {
         score = score +  scoreValue;
         UpdateScore();
     }

You are wanting to update the score when you destroy a asteroid, so probably just call the method UpdateScore() in your adScore () (btw you should name it AdScore instead since it is a method and not a variable, i.e Pascalcase for methods and camelcase for variables)

	public void adScore(int scoreValue)
	{
		score = score +  scoreValue;
		UpdateScore ();
	}

I think the problem you had before is that you just weren’t making an active call to the method when you wanted to update score text. Your asteroid’s OnTriggerEnter method only made a call to adScore().

Putting it in Unity’s built in Update() method makes it always update the score every frame, that is ok, but it is better to to just call your method only when you need it rather than every frame.