- Home /
Strange situation about WaitForSeconds
Hi there. I have two scripts, one of them derieved from "object", and other from "MonoBehaviour"
In first script I have this
public virtual MyCustom removeChild(MyCustom child)
{
if (child.target != null)
{
foreach (MyCustom elem in childsList)
if (elem.target == child.target)
{
int itemIndex = childsList.IndexOf(elem);
childsList[itemIndex].target.transform.SetParent(GameObject.Find("Disabled items").transform);
childsList[itemIndex].target.GetComponent<MyCustomGameObject>().SendMessage("StartOff");
childsList.Remove(elem);
break;
}
updateTree();
}
return child;
}
And in second script ("MyCustomGameObject, for example), I have this
public void StartOff() {
Debug.Log ("Worked");
StartCoroutine ("SetOff");
}
IEnumerator SetOff() {
Debug.Log ("log?");
yield return new WaitForSeconds(1f);
Debug.Log ("logged");
this.gameObject.SetActive (false);
}
And when I play, I get two debug messages - "Worked" and "log?".
So, here is a question:
Why my code runs only before "yield return new WaitForSeconds"???
Answer by cjdev · Sep 08, 2015 at 10:16 AM
Coroutines are only implemented in Monobehaviour objects so in your custom object when you return your new WaitForSeconds IEnumerator the function ends like it would in another function if you were to return any other value.
Dude, did u read my question? Script, that consider IEnumerator was derivied from $$anonymous$$onoBehaviour. Any other suggestions?
Answer by DiegoSLTS · Sep 08, 2015 at 03:22 PM
Make sure the object with MyCustomGameObject component is active.
Make sure the MyCustomGameObject component is enabled.
Make sure nothing removes that game object from the scene, or deactivates it or the component.
Make sure Time.timeScale is not 0.
A coroutine will only work with all that, deactivating or destroying a game object that started a coroutine will stop that coroutine at that moment, and if the objects was already deactivated the coroutine will run until the first return statement. Aslo, the WaitForSeconds return depends on the timeScale, if it's 0 the 1 second will never happen.
Your answer
![](https://koobas.hobune.stream/wayback/20220612032853im_/https://answers.unity.com/themes/thub/images/avi.jpg)