Hello everyone. I am creating a 2d Mobile, touchscreen game for unity. I would like to have a UI score increase every time I touch the object. When I touch the specified object there is a collision but I want to enable a score for when these objects collide when I touch them. I will select the best answer.
@putback This will work as a basis for what you want:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; //Required when using UI elements in the script.
public class ExampleScript : MonoBehaviour {
public int currentScore;
public Text displayScore;
// Invoked on collision
void OnCollisionEnter2D (Collision2D coll)
{
if (coll.gameObject.tag == "Player")
{
//Add 20 points each time the player collides
// with the object this script is attached to
currentScore += 20;
}
}
// Update is called once per frame
void Update ()
{
displayScore.text = currentScore.ToString();
}
}
@Ben Stoneman . I need help, for some reason the score count is staying at 0 and I keep getting the error: Null Reference Exception: Object reference not set to an instance of an object.
on lithis line: displayScore.text = currentScore.ToString();