Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Coroutine can't start since object is inactive and blabla

Discussion in 'Scripting' started by Rphysx, May 18, 2014.

  1. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Here's the guilty coroutine in EnemyAnim.cs

    Code (csharp):
    1.    
    2.   IEnumerator LeftRightMovement()
    3.     {
    4.     while (true)
    5.     {
    6.     for (int i = 0; i < 100; i++)
    7.     {
    8.     Debug.Log("executing");
    9.     transform.position = new Vector2(transform.position.x + y, transform.position.y);
    10.     yield return new WaitForSeconds(0.02f);
    11.     }
    12.     new WaitForSeconds(3f);
    13.     AssignMoveLeftRight(transform.position); //stops current coroutine without yield break and start a new instance of this one
    14.     }
    15.     }

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


    Code (csharp):
    1.  
    2.     public class EnemySpawner : MonoBehaviour {
    3.      
    4.     public Vector3 ThisPos;
    5.     public GameObject Enemy;
    6.     EnemyAnim enemyAnim;
    7.      
    8.      
    9.     void Start () {
    10.     ThisPos = transform.position;
    11.     enemyAnim = Enemy.GetComponent<EnemyAnim>();
    12.     Enemy.transform.position = ThisPos;
    13.     Instantiate(Enemy);
    14.     }
    15.      
    16.     void Update () {
    17.     if (Input.GetKeyDown(KeyCode.R))
    18.     { Instantiate(Enemy); enemyAnim.AssignMoveLeftRight(transform.position); } //Enemy.GetComponent<EnemyAnim>().AssignMoveLeftRight(transform.position); }
    19.     }
    20.  

    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!"
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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.
     
  3. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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 ?
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you make sure that the prefab itself is not deactivated, and thus the instances not as well?
     
  5. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you show the code where you start the coroutine?
     
  7. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    Code (csharp):
    1.  
    2.     public void AssignMoveLeftRight(Vector3 Position)
    3.     {
    4.         SpawnerPos = Position;
    5.         StopAllCoroutines();
    6.        
    7.         if (transform.position.x > Position.x + 2 || transform.position.x < Position.x - 2)
    8.         {
    9.             Debug.Log("true"); //Start Inverted direction Coroutine
    10.         }
    11.         else
    12.         {
    13.              int i = UnityEngine.Random.Range(0, 2);  //Start Coroutine RandomDirection
    14.              y = i == 0 ? 0.01f : -0.01f;
    15.              StartCoroutine("LeftRightMovement");
    16.         }
    17.     }
    18.  
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  9. Rphysx

    Rphysx

    Joined:
    Mar 26, 2014
    Posts:
    54
    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

    Code (csharp):
    1.  
    2.  var go = Instantiate(Enemy) as GameObject;
    3.             go.transform.parent = transform;
    4.             go.GetComponent<EnemyAnim>().AssignMoveLeftRight(transform.position);
    5.  
     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Are you kidding me? Why don't you show your actual code here?