- Home /
Gameobject's Script component doesn't start IEnumerator, nor InvokeRepeating when activated... Why?
I have a gameObject with a script on it that enables and disables (one at a time) SpriteRenderers using IEnumerator. Imagine dialog images showing up on a character. These Sprites (the dialog images) are children of gameObject. After all the Sprites finish the gameObject is set to destroy itself (also done with courutine).
This works perfectly if gameObject is enabled on game start. MY ISSUE is when a second gameObject with a different Script that handles different Sprites (second conversation with player) gets enabled after some player's conditions have been met (player collects something): this is when the IEnumerator doesn't work; only the first Sprite is enables (function of void OnEnabled). Here is the showText script for first gameObject (this courutine is counting very good): using UnityEngine; using System.Collections;
namespace UnityStandardAssets.ImageEffects { public class ShowText1 : MonoBehaviour {
public SpriteRenderer Dialog1;
public SpriteRenderer Dialog2;
public SpriteRenderer Dialog3;
public SpriteRenderer Dialog4;
public GameObject PLAYER;
public Rigidbody2D pl_Rigidbody2D;
public VignetteAndChromaticAberration vignette;
public RigidbodyConstraints2D constraints;
void Start()
{
Dialog2.enabled = false;
Dialog3.enabled = false;
Dialog4.enabled = false;
pl_Rigidbody2D = PLAYER.GetComponent<Rigidbody2D>();
vignette = Camera.main.GetComponent<VignetteAndChromaticAberration>();
vignette.enabled = true;
Destroy(gameObject, 19);
StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
//This is a coroutine
Dialog1.enabled = true;
pl_Rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
yield return new WaitForSeconds(3); //Wait one frame
Dialog1.enabled = false;
Dialog2.enabled = true;
yield return new WaitForSeconds(3);
Dialog2.enabled = false;
Dialog3.enabled = true;
yield return new WaitForSeconds(6);
Dialog3.enabled = false;
Dialog4.enabled = true;
yield return new WaitForSeconds(6);
Dialog4.enabled = false;
pl_Rigidbody2D.constraints = RigidbodyConstraints2D.None;
vignette.enabled = false;
}
}
} And this is the second gameObject's showText script (IEnumerator, nor InvokeRepeating doesn not start onEnabled): using UnityEngine; using System.Collections;
namespace UnityStandardAssets.ImageEffects { public class ShowText2 : MonoBehaviour {
public SpriteRenderer Dialog5;
public SpriteRenderer Dialog6;
public SpriteRenderer Dialog7;
public GameObject PLAYER;
public Rigidbody2D pl_Rigidbody2D;
public VignetteAndChromaticAberration vignette;
public RigidbodyConstraints2D constraints;
public int timerSeconds = 60;
void CountDown()
{
timerSeconds--;
if (timerSeconds < 1)
{
Dialog5.enabled = true;
pl_Rigidbody2D.constraints = RigidbodyConstraints2D.FreezePositionX;
}
if (timerSeconds < 5)
{
Dialog5.enabled = false;
Dialog6.enabled = true;
}
if (timerSeconds < 6)
{
Dialog6.enabled = false;
Dialog7.enabled = true;
}
if (timerSeconds < 6)
{
Dialog7.enabled = false;
pl_Rigidbody2D.constraints = RigidbodyConstraints2D.None;
vignette.enabled = false;
CancelInvoke("CountDown");
}
}
void OnEnabled()
{
InvokeRepeating("Countdown", 1, 1);
Dialog6.enabled = false;
Dialog7.enabled = false;
pl_Rigidbody2D = PLAYER.GetComponent<Rigidbody2D>();
vignette = Camera.main.GetComponent<VignetteAndChromaticAberration>();
vignette.enabled = true;
Destroy(gameObject, 19);
CountDown();
}
}
Here is the part on the player which activates second game object: void Update () { if (curRunes == 1 && Odin2 !=null) { wunjo = true; Odin2.SetActive(true); } Please help me find a solution where courutine works after gameObject gets enabled!
Answer by vintar · Dec 06, 2015 at 10:07 PM
Its a typo, my speciality :) it should be OnEnable()
Thanks, but I've tried OnEnable() and the second gameObject doesn't appear when player's conditions are met. It only becomes active if I write OnEnabled()... so it must be something else that messes with the Counter.
Your answer
Follow this Question
Related Questions
Execution Order of Scripts - Awake and OnEnable 1 Answer
A way to know if unity was just opened? [ExecuteInEditMode] 0 Answers
C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 1 Answer
My raycast wont function properly 0 Answers
How to create a score manager script involving awarding points from multiple objects? 2 Answers