- Home /
 
Unity Events executed out of order
According to this page Events in unity are suppose to execute in a specific order. Specifically, OnCollisionEnter is supposed to be called right after all of the OnTriggerEnter functions have been called on all objects; however, FixedUpdate and Update are both being called at least once in between calls to my OnTriggerEnter2D and OnCollisionEnter2D functions in my Enemy class.
Here is my code:
 public class Enemy : Combatant
 {
     public int kill_points;
     public float force;
     public List<GameObject> path_verticies;
     private int pathi;
     private bool on_path;
     private bool is_flying;
     private bool ignore_collision;            //Used for ignoring collisions with walls, if the enemy has already touched an enemy_controller.
     void Start ()
     {    Init();
         pathi = 0;
         ignore_collision = false;
         is_flying = false;
         if (gameObject.tag.Contains("Fly")) is_flying = true;//Assuming contains is cheaper than a full string compare.
         on_path = null != path_verticies && 0 != path_verticies.Count;
     }
 
     private void FixedUpdate() { ignore_collision = false; }
 
     void Update ()
     {    if (!CheckBounds()) Destroy(gameObject);
         if (on_path)
         {    transform.LookAt(path_verticies[pathi].transform, Vector3.up);
             transform.Rotate(new Vector3(0,-90,0));
             phys.velocity = transform.right * speed;
         }
         else dx = Mathf.Sign(Cx) * speed;
     }
 
     public void Flip(){ Cx *= -1; }
 
 #pragma warning disable CS0642 // Possible mistaken empty statement
     private IEnumerator OnCollisionEnter2D(Collision2D collision)
     {    if (on_path && path_verticies[pathi] == collision.gameObject)
         {    if (collision.gameObject == player)
             {    throw new System.NotImplementedException();
                 yield return new WaitForSeconds(0.1f);//Used to prevent damage to the player if the player landed on top of the enemy.
                 var atk = player.GetComponent<Combatant>().vigor.Attack(2);
                 if (Health.Atk.DIES == atk) player.GetComponent<Life>().Die();
             } else pathi = (pathi+1) % path_verticies.Count;
             yield return new WaitForSeconds(0.0f);
         }
         else if (on_path)
         {    if (collision.gameObject == player)
             {    yield return new WaitForSeconds(0.1f);//Used to prevent damage to the player if the player landed on top of the enemy.
                 var atk = player.GetComponent<Combatant>().vigor.Attack(2);
                 if (Health.Atk.DIES == atk) player.GetComponent<Life>().Die();
             } else if (collision.collider == On(Truebounds)) /*nothing*/;
             else if (collision.gameObject.layer == 0 || collision.gameObject.layer == 8)// TODO
                 /*take velocity in the direction opposite of the normal and add it to
                  * the perpendicular component the enemy is moving in*/;
             yield return new WaitForSeconds(0.0f);
         }
         else
         {    if (collision.gameObject == player)
             {    yield return new WaitForSeconds(0.1f);//Used to prevent damage to the player if the player landed on top of the enemy.
                 var atk = player.GetComponent<Combatant>().vigor.Attack(2);
                 if (Health.Atk.DIES == atk) player.GetComponent<Life>().Die();
             } else if (ignore_collision) /*nothing*/;
             else if (collision.collider == On(Truebounds)) { if (is_flying) Flip(); }
             else if (collision.gameObject.layer == 0 || collision.gameObject.layer == 8) Flip();
             yield return new WaitForSeconds(0.0f);
         }
     }
 
     private IEnumerator OnTriggerEnter2D(Collider2D collider)
     {    var component = collider.GetComponent<MonoBehaviour2>();
         if(!component) yield return null;
         else if (on_path && path_verticies[pathi] == collider.gameObject)
             pathi = (pathi+1) % path_verticies.Count;
         else if (on_path) /*nothing*/;
         else if (component.type == typeof(Enemy_Controller)) { Flip(); ignore_collision = true; }
         yield return null;
     }
 #pragma warning restore CS0642 // Possible mistaken empty statement
 
     //public void OnDestroy(){  }
 }
 
               And these are my timestep settings: 
Your answer
 
             Follow this Question
Related Questions
Can't reproduce some physics sequence 1 Answer
Altering Fixed Timestep when your game is running to allow for smooth low Timescale animation. 2 Answers
Is it possible to tick the fixed timestep manually or increase the timescale beyond 100? 2 Answers
Javascript Timescale Problem 0 Answers
Can i ignore timescale when playing AudioSource.PlayClipAtPoint ? 1 Answer