Coroutine can't start since object is inactive and blabla

Here’s the guilty coroutine in EnemyAnim.cs

  IEnumerator LeftRightMovement()
    {
    while (true)
    {
    for (int i = 0; i < 100; i++)
    {
    Debug.Log("executing");
    transform.position = new Vector2(transform.position.x + y, transform.position.y);
    yield return new WaitForSeconds(0.02f);
    }
    new WaitForSeconds(3f);
    AssignMoveLeftRight(transform.position); //stops current coroutine without yield break and start a new instance of this one
    }
    }

I’m trying to start it from another script called EnemySpawner.cs this way :

    public class EnemySpawner : MonoBehaviour {
     
    public Vector3 ThisPos;
    public GameObject Enemy;
    EnemyAnim enemyAnim;
     
     
    void Start () {
    ThisPos = transform.position;
    enemyAnim = Enemy.GetComponent<EnemyAnim>();
    Enemy.transform.position = ThisPos;
    Instantiate(Enemy);
    }
     
    void Update () {
    if (Input.GetKeyDown(KeyCode.R))
    { Instantiate(Enemy); enemyAnim.AssignMoveLeftRight(transform.position); } //Enemy.GetComponent<EnemyAnim>().AssignMoveLeftRight(transform.position); }
    }

Inside the AssignMoveLeftRight(vector3 vector) method there’s the line that asks to start the coroutine, the method resides in EnemyAnim.cs

Log error sais “Coroutine couldn’t be started because the the game object ‘Attack’ is inactive!”

Coroutines can only be executed within scripts that are active. That means the script is enabled and the game object has to be active. This includes all parent game objects.

So If I instantiate a prefab before calling the coroutine this doesn’t automatically set the state of that object to active ? what should I do then ?

Did you make sure that the prefab itself is not deactivated, and thus the instances not as well?

as soon as I instantiate the enemy gets on the screen and display idle animation properly but that coroutine is still broken. By the way I solved assigning a new var to the instantiated prefab and then calling that function on the referenced var and works fine somehow

Could you show the code where you start the coroutine?

    public void AssignMoveLeftRight(Vector3 Position)
    {
        SpawnerPos = Position;
        StopAllCoroutines();
       
        if (transform.position.x > Position.x + 2 || transform.position.x < Position.x - 2)
        {
            Debug.Log("true"); //Start Inverted direction Coroutine
        }
        else
        {
             int i = UnityEngine.Random.Range(0, 2);  //Start Coroutine RandomDirection
             y = i == 0 ? 0.01f : -0.01f;
             StartCoroutine("LeftRightMovement");
        }
    }

You have to instantiate the prefab and start the coroutine within that created instance. Have a look at the second example here to see how you can get the instance:

I’m doing Instantiate(Enemy) in the 17th line in the second class from the first post. I can’t exactly understand why (which is pretty bad) but to have this to work I need to reference the prefab instance to another gameobject created as soon as I instantiate the enemyPrefab it self, here’s how

 var go = Instantiate(Enemy) as GameObject;
            go.transform.parent = transform;
            go.GetComponent<EnemyAnim>().AssignMoveLeftRight(transform.position);

Are you kidding me? Why don’t you show your actual code here?