Skip to main content
typo
Source Link
Exerion
  • 685
  • 1
  • 7
  • 17

Right after Instantiation of bf object bf.OnEnable() method was called causing the object to do some code and rotate. After that bf.SetActive (false) is called, but it's too late.

After that run your code and you will see sequence of messages in the console. I suppose that between 'Instantiation''Instantiation' and 'Deactivation''Deactivated' will be 'Turned' message.

Right after Instantiation of bf object bf.OnEnable() method was called causing the object do some code and rotate. After that bf.SetActive (false) is called, but it's too late.

After that run your code and you will see sequence of messages in the console. I suppose that between 'Instantiation' and 'Deactivation' will be 'Turned' message.

Right after Instantiation of bf object bf.OnEnable() method was called causing the object to do some code and rotate. After that bf.SetActive (false) is called, but it's too late.

After that run your code and you will see sequence of messages in the console. I suppose that between 'Instantiation' and 'Deactivated' will be 'Turned' message.

Source Link
Exerion
  • 685
  • 1
  • 7
  • 17

The trick about pooling complex/customizable objects is to reset their state before returning to a pool. So when you fetch from a pool, fetched object always be the same as instantiated one. I don't see any code that return object to pool.

I may suppose that when you init your pooler component some of AI get working before you disable objects. Between this two lines:

        GameObject bf = (GameObject)Instantiate (BF,Vector3.zero,Quaternion.identity);
        bf.SetActive (false);

Right after Instantiation of bf object bf.OnEnable() method was called causing the object do some code and rotate. After that bf.SetActive (false) is called, but it's too late.

To check this out, add some debug code to your EnemiesPooler.Start() method:

void Start ()
{
    BFPlane = new List<GameObject> ();
    JUPlane = new List<GameObject> ();
    HawkerPlane = new List<GameObject> ();
    BlenheimPlane=new List<GameObject>();
    current = this;

    for (int i=0; i<pooledAmount; i++) {
        Debug.Log("Instantiation"); // here
        GameObject bf = (GameObject)Instantiate (BF,Vector3.zero,Quaternion.identity);
        Debug.Log("Instantiated. Deactivation"); // here
        bf.SetActive (false);
        Debug.Log("Deactivated"); // and here
        BFPlane.Add (bf);

        GameObject ju = (GameObject)Instantiate (JU,Vector3.zero,Quaternion.identity);
        ju.SetActive (false);
        JUPlane.Add (ju);

        GameObject hawker = (GameObject)Instantiate (Hawker,Vector3.zero,Quaternion.identity);
        hawker.SetActive (false);
        HawkerPlane.Add (hawker);

        GameObject blenheim = (GameObject)Instantiate (Blenheim,Vector3.zero,Quaternion.identity);
        blenheim.SetActive (false);
        BlenheimPlane.Add (blenheim);

    }

}

And in the AI.OnEnable() method:

void OnEnable ()
{  
    transform.rotation = Quaternion.Euler (Vector3.zero);
    rb = GetComponent<Rigidbody2D> ();
    turnVel = new Vector2 (0, -0.5f);
    if (transform.position.x < 0) {
        leftSpawn = true;
        transform.Rotate (0, 0, -90);
        Debug.Log("Turned -90*"); // here
        vel = new Vector2 (1, 0);
    }

    if (transform.position.x > 0) {
        rightSpawn = true;
        transform.Rotate (0, 0, 90);
        Debug.Log("Turned 90*"); // and here
        vel = new Vector2 (-1, 0);
    }
}

After that run your code and you will see sequence of messages in the console. I suppose that between 'Instantiation' and 'Deactivation' will be 'Turned' message.