Need help with using Destroy with a variable condition
Currently in my code, I have it so when the player is on the ground and a variable (isTriggered) equals 0 and a key is pressed. Then and only then does the object the player is on get destroyed.
The issue is that half the time it doesn't seem to matter whether or not isTriggered equals 0, it just does it anyway and the key is pressed. I think it's due to isTriggered not being switched to 0 faster than the player can press the key but I can't find anyway to make sure isTriggered equals 0 other than the way I have it atm
If anyone could take a look and see where I went wrong, that would be great
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockRemoval : MonoBehaviour {
public int isTriggered;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.CompareTag ("Dirt")) {
if (isTriggered == 0) {
if (Input.GetKey (KeyCode.S)) {
Destroy (coll.gameObject);
}
}
}
}
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.CompareTag ("Trigger"))
isTriggered = 1;
else {
isTriggered = 0;
}
print (isTriggered);
}
void OnTriggerExit2D(Collider2D trigg)
{
if (trigg.gameObject.CompareTag ("Trigger"))
isTriggered = 0;
else {
isTriggered = 1;
}
print (isTriggered);
}
}
Comment