Hello,
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;
Destroy(gameObject);
}
GameManager:
public class playerScore : MonoBehaviour {
public int score = 0;
public Text scoreTxt;
void Update()
{
scoreTxt.text = score.ToString();
}
Thank you!