- Home /
Coroutine Interface
Hi, I have a coroutine that try to change some variable in other script(TestUnit ) via interface function and when i debug.log that variable inside the TestUnit script in its update function i get" true,false,true,false like if i have some race condition there. any idea why is that?
using UnityEngine; using System.Collections;
interface IDummy { void SetAction( bool act ); void KillRoutine(); }
//assume this class is attached to object A public class TestUnit : MonoBehaviour, IDummy { private bool inAction = false;
public void TouchRelease ( )
{
if( inAction )
return;
StartCoroutine( "Test" );
}
IEnumerator Test()
{
if( inAction==false )
{
inAction = true;
float t = 0;
while( t < 2f )
{
t+= Time.deltaTime;
yield return null;
}
inAction = false;
}else{
yield break;
}
}
public void SetAction (bool act)
{
inAction = act;
}
public void KillRoutine ()
{
StopCoroutine("Test");
}
void Update()
{
Debug.Log( inAction ); //PROBLEM it's print: True,False,True,False........
}
}
//assume this class is attached to object B public class Counter: MonoBehaviour { public static bool inCount = false;
void OnTriggerEnter( Collider other )
{
StartCoroutine( CountIT( other.transform ) );
}
IEnumerator CountIT( Transform obj )
{
while( Counter.inCount == true )
{
yield return null;
}
Counter.inCount = true; //prevent other same
IDummy dummy = (IDummy)obj.GetComponent(typeof(IDummy));
if( dummy!= null )
{
dummy.KillRoutine();
dummy.SetAction( true );
}
float t = 0;
while( t < 7f )
{
t+= Time.deltaTime;
yield return null;
}
if( dummy!=null )
dummy.SetAction( false );
Counter.inCount = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613064317im_/https://answers.unity.com/themes/thub/images/avi.jpg)