0

Regarding the assignment here

To display the score I added a Text UI in the canvas and added a SCORE component.

I created a class for doing the job. Here is the class.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public static int score = 0;
    private Text scoreText;
    // Start is called before the first frame update
    void Start()
    {
        scoreText = GameObject.Find("Score").GetComponent<Text>();
        scoreText.text = "Score: " + score;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

And to increase the score, I have done that in GrabPickups.cs here it is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GrabPickups : MonoBehaviour {

    private AudioSource pickupSoundSource;

    void Awake() {
        pickupSoundSource = DontDestroy.instance.GetComponents<AudioSource>()[1];
    }

    void OnControllerColliderHit(ControllerColliderHit hit) {
        if (hit.gameObject.tag == "Pickup") {
            pickupSoundSource.Play();
            SceneManager.LoadScene("Play");
            Score.score += 1;
        }
    }
}

However, the score increments abnormally. It sometimes increments by 5, sometimes 3 sometimes 4 and sometimes 1, how to fix that?

You must log in to answer this question.

Browse other questions tagged .