- Home /
Question by
pro2008player · May 05, 2019 at 02:52 AM ·
c#programmingscorescore system
Add bonus to score
Hi, so I'm new to unity and C# and am trying to add a bonus to my score (score measures the players z position) and really don't really know how so a little help would be really nice. Heres what I have
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Transform player;
public Text scoreText;
private int _score;
private int baseScore;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString("0");
if (player.position.y >= 2.5)
{
_score += 50;
scoreText.text = _score.ToString();
Debug.Log(scoreText.text);
// scoreText.text = player.position.z.ToString(scoreText.text + 100);
// scoreText.text = scoreText.text + player.position.z.ToString();
}
// Debug.Log(scoreText.text);
}
}
What happens is that it literally adds 50 to the end. Good luck and thank you!
Comment
Best Answer
Answer by Hellium · May 05, 2019 at 09:28 AM
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Transform player;
public Text scoreText;
private float score;
private float scoreBonus;
// Update is called once per frame
void Update()
{
score = player.position.z ;
if (player.position.y >= 2.5)
scoreBonus = 50;
scoreText.text = (score + scoreBonus).ToString("0");
}
}