Problem with player Score


I’m having issues with the player score for when they pick up collectables in my game.

Basically, in my game collectables only appear after the player travels a certain distance and are therefore spawned in with a script, now I want the players score to be kept within a game manager object.

Basics of what I need help with:

  • Player collides with coin obj
  • Coin script takes collision and stores the coins value
  • Coin script updates the score store within the game manager object
  • game manager script updates the score UI

There are multiple coin objects in the scene that are constantly spawning in

Can anyone help me with a script that would update the players score that’s stored on the game manager object when they collide with the collectables, even when there are multiple collectables in the scene?

My code so far; C# :

public class coin : MonoBehaviour {

    public GameObject gmScoreTxt;
    public int value = 1;
    private int count = 0;
    public int playerScoreNum = 0;

    public int setScore = 0;

    void Start()
        gmScoreTxt = GameObject.Find("gameManager");

    void OnTriggerEnter(Collider collision)
        if (collision.gameObject.tag == "Player")
            count = count + value;
            playerScoreNum = count;
            setScore = gmScoreTxt.GetComponent<playerScore>().score;
            setScore = playerScoreNum;


public class playerScore : MonoBehaviour {

    public int score = 0;
    public Text scoreTxt;

    void Update()
        scoreTxt.text = score.ToString();


Thank you!

Your code looks pretty good so far. Have you tried running the game and seeing if it works? One comment I have right away is that you do not have to use the toString() method with the score you can simply use score.

public Text scoreText;
public long score;

void Update () {
    scoreText.text = "Score: " + Mathf.Round(score); // Doesnt have to be exactly like this. 

As for your coin class, you are setting setScore to equal something twice, so the first line in which you set the score to is unnecessary. PlayerScoreNum is also unnecessary because you can just use count and set the score equal to that. You also have to change your score in the score manager using the coin class. You can do this by creating an object of playerScore in the coin class. Make sure you drag the score manager that you create into the public field setScore in the coin class.

playerScore setScore;

if (collision.gameObject.tag == "Player")
         setScore.score =+ value;

I hope this works. If it does not feel free to let me know and I will do my best to help!