I need help with a click to destroy game
I have no clue what I am doing, and need help to try and make a simple click and destroy game for my coursework. I have gotten as far as adding 2d coliders to my objects and a simple script (that is bellow) that makes the objects disappear with one click.
How do I make it so the object takes multiple clicks to destroy?
using UnityEngine; using System.Collections;
public class Destroy : MonoBehaviour {
void OnMouseDown(){ Destroy(gameObject); }
}
Answer by alankemp · May 09, 2017 at 08:13 AM
At the moment you are destroying the object as soon as the user clicks it.
Instead you could have an int as a member of your class that counts how many times it has been clicked, and when it goes over a certain amount then it gets destroyed.
Maybe something like this:
public class Destroy : MonoBehaviour
{
public int NumberOfClicksToDestroy = 3; // set this here or in Unity
// a counter for how many times we have been clicked
private int ClicksCount;
void OnMouseDown()
{
// they clicks this object, increase its click count
ClicksCount++;
// if this has been clicked enough times, destroy it
if (ClicksCount > NumberOfClicksToDestroy)
{
Destroy(gameObject);
}
}
}