Problem with player Score
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!
Answer by abeLincoln41 · May 14, 2017 at 07:14 AM
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;
Destroy(gameObject);
}
I hope this works. If it does not feel free to let me know and I will do my best to help!
Hey, thanks for the reply @abeLincoln41.
I've tried this and it still doesn't want to work, as soon as I collide with one of the coins I get this error:
NullReferenceException: Object reference not set to an instance of an object coin.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/scripts/coin.cs:29)
Your answer
Follow this Question
Related Questions
Spawn Object on Set Score 0 Answers
when i reload the scene the score number remains the same and doesnt revert back to zero 1 Answer
In updating some score text, why should we use the Update function instead of separate functions? 0 Answers
Help making a high score with player prefs/displaying it 1 Answer
Can't update score text - small error breaks the game (Space Shooter Tutorial) 1 Answer