Hello :) I am sorta new to code, and I'm trying to create a simple thing where there is a number counter for button pushing. The counter starts at 0, and every time you push the button on your keyboard (Right arrow key) the number goes up by one. Below is what I have so far, and it's not working. Can anyone help??
public class Score : MonoBehaviour
private int score = 0;
public Text scoreText;
void Update ()
if (Input.GetKeyDown (KeyCode.RightArrow) )
scoreText.text = "Taps: " + score.ToString();
Answer by Pengocat
6 days ago
The code is working. All you need to remember is to drag a Text Component reference onto the "scoreText" field in the inspector.
If you want to do this by code instead, and the Text component is on the same GameObject as this script then all you need to add is the following.
scoreText = GetComponent<Text>();
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
7 People are following this question.
Multiple Cars not working
Distribute terrain in zones
An OS design issue: File types associated with their appropriate programs
Illuminating a 3D object's edges OnMouseOver (script in c#)?
Renderer on object disabled after level reload