- Home /
Destroy object after clicking button few times.
I want to write a script that will destroy a tree when three is clicked 3 times but script is stopping on "hp = 3". Thank you for help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SCINANIE : MonoBehaviour
{
public Animator animacja;
public Animator anim;
public Canvas text;
public Canvas textno;
private bool intree;
public Canvas A, B, C, D;
public GameObject axe, player, drzewo;
// Use this for initialization
void Start()
{
text.enabled = false;
textno.enabled = false;
intree = false;
animacja.enabled = false;
A.enabled = false;
B.enabled = false;
C.enabled = false;
D.enabled = false;
anim.enabled = false;
axe.SetActive(true);
}
// Update is called once per frame
void FixedUpdate()
{
}
void OnTriggerStay(Collider other)
{
var item = player.GetComponent<items>();
if (other.tag == "Player")
{
if (item.axe == true)
{
text.enabled = true;
textno.enabled = false;
intree = true;
Debug.Log("In trigger.");
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Invoke("one", 0.1f);
textno.enabled = false;
}
else
{
text.enabled = true;
textno.enabled = false;
}
}
else
{
text.enabled = false;
textno.enabled = true;
}
}
}
void one()
{
A.enabled = false;
B.enabled = false;
C.enabled = true;
D.enabled = false;
Debug.Log("hp = 3");
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Invoke("two", 0.1f);
}
}
void two()
{
A.enabled = false;
B.enabled = true;
C.enabled = false;
D.enabled = false;
Debug.Log("hp = 2");
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Invoke("three", 0.1f);
}
}
void three()
{
A.enabled = true;
B.enabled = false;
C.enabled = false;
D.enabled = false;
Debug.Log("hp = 1");
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Invoke("four", 0.1f);
}
}
void four()
{
A.enabled = false;
B.enabled = false;
C.enabled = false;
D.enabled = false;
Debug.Log("hp = 0");
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Invoke("six", 0.1f);
}
}
void six()
{
A.enabled = false;
B.enabled = false;
C.enabled = false;
D.enabled = false;
animacja.enabled = true;
anim.enabled = false;
text.enabled = false;
textno.enabled = false;
Invoke("Cut", 9.7f);
}
void OnTriggerExit(Collider other)
{
intree = false;
text.enabled = false;
textno.enabled = false;
}
void Cut()
{
Destroy(gameObject);
}
}
Answer by TwinGhosts · Aug 11, 2017 at 10:25 PM
You are trying to check a keyPress while the method is only called once by the Invoke(), you'd need to press the key the exact moment when the invoke is happening right now.
You could fix this by giving the object a general HP value and decrease this with each mouseClick. You could also execute more code for each HP value in this way as well.
An example:
if (Input.GetMouseButtonDown(0) && hp > 0){
hp--;
}
switch(hp){
case 0:
// Death actions here
break;
case 1:
// Actions here
break;
case 2:
// Actions here
break;
//ETC.
}
Or you could make a click counter and increment it each time the player presses the mousebutton at the object's position, like so:
int clickCount = 0;
if(Input.GetMouseButtonDown(0)){
clickCount++;
}
if(clickCount >= 3){
//Do stuff
}
Your answer
Follow this Question
Related Questions
mouse clicked on object animation c# 1 Answer
create a button with name and text for every game object with tag 0 Answers
Scripting button functionality inside class with created object's method 1 Answer
Dynamic Buttons, SciptableObjects and Lists 1 Answer
Alerts 3D game mobile controls with UI 0 Answers