- Home /
Coroutine couldn't be started because the the game object 'Attack' is inactive!
Hi everyone, this question is a bit long, but please bear with me.
I'm creating a game with a combat system very similar to final fantasy 10. I'm a big fan of keeping my code as neat as possible, so my characters all derive from a basic character class. that class contains a List of four abilities (as each character can only have four), which is of the type basicSkill, like this
List<basicSkill> Skills = new List<basicSkill>();
I then created an empty prefab named Attack (for a basic attack) with only basicSkill as it's component. I then dragged this prefab into the List of skills in a characters' component.
within every skill is a Run() function. it's always named run so that no matter what skill it is, you can run it straight from the character's skills list by it's index in the list.
Run is seen below
public void Run (GameObject attacker, GameObject target, _DefaultCharacter attackerStats, _DefaultCharacter targetStats){
_damage = attackerStats.Damage / targetStats.armor;
if (isMelee == true){
StartCoroutine(MeleeSequence());
}
}
IEnumerator MeleeSequence(){
yield return new WaitForSeconds(3);
Debug.Log(name + "Sequence Complete");
}
Once I had that sorted out, I created a button in order to test out my attack by having first character and enemy in the list as parameters. I was able to run attack through a long and convoluted line which you can see below.
if (GUI.Button(new Rect(10,10,50,20), "test attack")){
allies[0].GetComponent<_DefaultCharacter>().newSkills[0].Run(allies[0], enemies[0], allies[0].GetComponent<_DefaultCharacter>(), enemies[0].GetComponent<_DefaultCharacter>());
}
The problem is, I keep getting the error seen below;
Coroutine couldn't be started because the the game object 'Attack' is inactive!
I believe that the problem lies in the fact that it is a prefab that I drag to the inspector window on skills. when I have the same prefab in the scene and try to drag it into the inspector it won't allow it, though I don't see why it would accept it as a prefab. The whole reason I've done this is because I've tried to stop using dynamic calls, though I don't have any other ideas.
What might be causing the error?
Hmm, it looks like the coroutine couldn't be started, because the the game object 'Attack' is inactive.
I'm new to both program$$anonymous$$g and unity. I'm sorry if the error seemed obvious, but it really isn't to someone with as little experience as me. I didn't know that you can't run functions or coroutines from a prefab that hasn't been added to the scene so "Inactive" could mean any number of things to me
Answer by Bunny83 · May 09, 2013 at 09:18 AM
Coroutines are like you might know objects and not functions. Those "objects" have to be processed by Unity's coroutine scheduler. Coroutines are always bound to a MonoBehaviour which will hold a reference internally to all active coroutines and are responsible for processing them. If you call StartCoroutine you just pass the coroutine object to the scheduler. Of course the MonoBehaviour needs to be an active scene object in order to schedule coroutines.
So you should either:
instantiate your skill prefabs as childs of your player (maybe in the characters Start function)
Run the coroutine on a different Monobehaviour.
It seems your _DefaultCharacter class is a MonoBehaviour which is actually an instance in the game. You can run the coroutine on this MonoBehaviour:
attackerStats.StartCoroutine(MeleeSequence());
Co$$anonymous$$' to you from the future Bunny83 to give a big big thanks. Looked forever to find out that coroutine has to be run on a $$anonymous$$onobehaviour. $$anonymous$$an I wish this thread was more popular and that I understood this crap more.
Your answer

Follow this Question
Related Questions
My game freezes whenever i use a function which involves http? 0 Answers
Coroutines IEnumerator not working as expected 2 Answers
Generating Objects at High Speeds 0 Answers
How to randomly spawn object without spawning the previous object in a row? 0 Answers
WaitUntil doesn't work and the coroutine starts anyways 1 Answer