two objects 1 and 2; in 2. object I am calling a public function from the 1. object. however, in the 2. object I have a coroutine which should cancel when I call this public function I mentioned above.
I tried using StopCoroutine but it just pauses it. So i use a reload variable to check if the coroutine should work or not. is there any better way to do this?
OBJECT 1
private bool fire = true, reload = false;
//In the update lets say i call the coroutine "Reset_Fire" and set the "fire" to false
public void Reset_Attack()
{
fire = true;
reload = false;
Debug.Log("MO - 1");
//when this function is called.
//without the "reload" bool, it prints two "CO - 2" strings from "Reset_Fire"
//with the "reload" bool it prints "CO - 2" once but it shouldnt ever print it.
}
IEnumerator Reset_Fire()
{
yield return new WaitForSeconds(1f);
if (reload == true)
{
Debug.Log("CO - 2");
fire = true;
reload = false;
}
}
OBJECT 2
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
}
private void Take_Damage(int dmg)
{
hp -= dmg;
player.Reset_Attack();
if(hp <= 0)
{
Destroy(gameObject);
}
}
//the object tagged as "Light" isnt the 1. object or its child!!!
private void OnTriggerEnter2D(Collider2D col)
{
if(col.tag == "Light" && hurt == false)
{
hurt = true;
Take_Damage(1);
StartCoroutine(Reset_Hurt());
}
}
IEnumerator Reset_Hurt()
{
yield return new WaitForSeconds(0.5f);
hurt = false;
}