Unity 3D Game Engine – Long Touch – Counter Increase
The counter will increase if you tap or long touch the device
1. Inside the Hierarchy create the structure:
– Main Camera
– GUI Text
– GameController (Empty Object), attach the ‘TouchController.js’
TouchController.js:
#pragma strict // Hierarchy DRAG E DROP over var GUI Text in Inspector var scoreText : GUIText; // touch counter, private because the users is not be able to change this value private var score : int; function Start () { // The counter initial value is 0 score = 0; scoreText.text = "No Touch:"; } function Update() { if (Input.touchCount == 1) { // var score will increase if you tap or long touch the device UpdateScore (); } } function UpdateScore () { // score var increment of +1 score += 1; // scoreText in assigned inside Inspector on GUI Text // change .text property and write it on the display scoreText.text = "Touched: " + score; }
Hierarchy> GameController> TouchController.js assign over var Score Text the GUI Text Object