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?

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());