- Home /
Why won't my coroutine start?
I have a script which uses iTween to display experience gained from an enemy which is kicked off by a health script attached to the enemy. Here are the relevant parts of the script:
EnemyHealth.js attached to enemy - this happens upon death
var aRotation = Quaternion.Euler(0 , 0 , 0); Instantiate(xpText, transform.position, aRotation); print(myExpText); xpText.GetComponent(XPText).ChangeText(myExpText); xpText.GetComponent(XPText).DisplayXP();
Network.Destroy(gameObject);
XPText.js (attached to a 3DText prefab called XPText)
function DisplayXP() {
yield WaitForSeconds(1);
print(textContent);
GetComponent(TextMesh).text = textContent;
iTween.FadeTo(gameObject, 1, 0.5);
yield WaitForSeconds(2);
iTween.FadeTo(gameObject, 0, 0.5);
yield WaitForSeconds(1);
Destroy(gameObject);
}
function ChangeText(valueA) { textContent = valueA.ToString() + "XP"; }
Please note that Network.Instantiate is intentional.
When I kill an enemy I get the following error:
"Coroutine couldn't be started because the the game object 'XPText' is inactive!"
So I removed the "yield WaitForSeconds" lines but then I get the error:
Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object)
Any ideas how what I've done wrong or any way around this?
Answer by Mike 3 · Apr 07, 2011 at 10:11 PM
You're trying to start the coroutine on the prefab, not the instantiated object
Try something like this:
var instantiated = Instantiate(xpText, transform.position, aRotation);
print(myExpText);
instantiated.GetComponent(XPText).ChangeText(myExpText);
instantiated.GetComponent(XPText).DisplayXP();
Answer by DaveA · Apr 07, 2011 at 09:39 PM
Just a quick thought: If the XPText it refers to is on this line:
Instantiate(xpText, transform.position, aRotation);
then make sure xpText is active by either setting .active = true or checking the box on it in the Inspector.
When I add xpText.gameObject.active = true; I get an error saying: "Prefab GameObject's can not be made active! (XPText)". xpText is a Prefab for a 3DText GameObject with the XPText script attached to it. Basically when the enemy dies I want it to instantiate the 3DText with a fade effect (hence iTween).