Score Increasing

Hi, I would like to make the score increase while I’m touching the screen (Mobile control):

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour {

public Transform player;
public Text scoreText;

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

	scoreText.text = player.position.z.ToString("0");

    if (Input.GetKey("b"))
    {
        scoreText.text += 1000;
    }
}

}

But the score doesn’t increase, for example is the z position is 23 it become 231000 (It’s not added but placed next to it)

scoreText.text is a string and you’re adding 1000, an integer. So yes, it will append 1000 to the end of it. If scoreText.text is 23, then adding 1000 will make it 231000, so your example makes sense.

Anyway, when a button or the screen is pressed, increment the score by whatever value you want or by Time.deltaTime if you want to increase the score per second, e.g., add 1 to score per second (you’ll have to round the decimal value to a reasonable number though).

  • public class ScoreIncreasing : MonoBehaviour
    {
    public Transform player;
    public Text scoreText;
    public int score;
    public int value;

      void Update ()
      {
      	if ( Input.GetKey( KeyCode.B ) )
          {
              score += value;
              scoreText.text = score.ToString( "0" );
          }
      }
    

    }

What’s confusing is you’re setting the text to the player’s Z position, so why do you need to increment the score when a button is down if it’s already being updated based on player position. If that’s what you want you’d just do:

  •   void Update()
      {
          if ( Input.GetKey( KeyCode.B ) )
          {
              scoreText.text = player.transform.position.z.ToString( "0" );
          }
      }