- Home /
My coroutine wont stop working if I am stopping it from another script
So I have this code
public void showUnit()
{
StopCoroutine("hideAgain");
StartCoroutine("hideAgain");
currentColor = detectedColor;
sr.color = currentColor;
transform.parent = alliedUnits;
}
IEnumerator hideAgain()
{
yield return new WaitForSeconds(1f);
hideUnit();
Debug.Log("I am called");
}
Which works as expected when I do something like this
private bool showUnitBool = false;
private void Update()
{
if(Input.GetKeyDown("s"))
{
showUnitBool = !showUnitBool;
}
if (showUnitBool) showUnit();
}
Basically what this code does is make the unit appear on screen whenever show unit is called and if the show unit function stopped being called it takes 1 second then disappears on its own all is good so far until I call this script from another script/gameObject like so
private void Update()
{
foreach (Transform enemy in enemyInvisibleUnits)
{
if (unitIsClose)
{
enemy.GetComponent<InvisibleUnit>().showUnit();
}
}
}
Now what happens is "I am called" string is logged every second and my sprite starts flickering meaning it goes invisible every second for about 1-3 frames, What I am saying is its like StopCoroutine is never being called although it worked when I called it in the update function by clicking on s to stop calling or start calling showUnit(), Whats more interesting is I tried the same thing with Invoke and CancelInvoke() functions the same problem happens, Also I tried putting my Coroutine in an IEnumerator variable the same problem happens although it works fine in the update version of it so any help?
Your answer
![](https://koobas.hobune.stream/wayback/20220612143738im_/https://answers.unity.com/themes/thub/images/avi.jpg)