The base method IEnumerator OnThink
you are overriding, overriding means you want to implement all the logic yourself instead of what the method class provides
//base
protected virtual IEnumerator OnThink(float interval)
{
while (true)
{
yield return new WaitForSeconds(interval);
}
}
//so this function is now overridden meaning it has no logic
//unless you add it or call the base
//to make it work like the base implementation you just add the while loop
protected override IEnumerator OnThink(float interval)
{
while(true)
{
Debug.Log("Execute");
yield return StartCoroutine(base.OnThink(interval));
}
}
This particular while loop will run forever since true always will equal true.
while(true) is short for hand for while(true == true) so will run forever, if you add your own boolean for example
bool canThink = false;
then use that instead of true = true like so
private bool canThink = false;
protected virtual IEnumerator OnThink(float interval)
{
while (true)
{
yield return new WaitForSeconds(interval);
}
}
protected override IEnumerator OnThink(float interval)
{
//for every X interval if canthink is true run the code inside the loop
while(canThink == true)
{
Debug.Log("Execute");
//we also just call yield return new WaitForSeconds ourselves
//in this case there isnt much point in calling the base method.
yield return new WaitForSeconds(interval);
}
}
This way you control the flow of the logic, you can stop and start it just by changing canThink
- bare in mind you probably don't want to be calling base in your case as a infinite loop inside a infinite loop will either crash or slow things down a fair bit.