Destroy a game object after holding Mouse 0 for some time
This my script that I attach to each square (It's a 2D game)
Blockquote
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Destroy : MonoBehaviour {
public float counter;
public float timeToDestroy = 1;
void FixedUpdate () {
if (Input.GetMouseButtonDown(0)) {
counter += Time.deltaTime;
print ("counter: " + counter);
if (counter >= timeToDestroy) {
Destroy (this.gameObject);
print ("Destroy!");
} else {
counter = 0;
print ("counter: " + counter);
}
}
}
}
Blockquote
sorry for my bad english xD
If I set my "timeToDestroy" to 0, then it's work, but not if i set it to 5 or 3 or 6 or 1.5 or ...
Answer by Menyus777 · Mar 20, 2018 at 10:09 PM
lemme translate your computer to you:
(counter = 0) >= (TimetoDestroy = 5) this is false Master lemme check the else statement
Checking the else statement master
i set the counter to 0 brah
Checking again the if statement brah
(counter = 0) >= (TimetoDestroy = 5) this is false Master lemme check the else statement
repeat to infinity
Ps: time.DeltaTime is the amount of time needed to finish your last frame your GameObject will only be destroyed if the last frame took more than 5 seconds