Search Unity

Question Let one gameobject trigger before another

Discussion in 'Scripting' started by Darkkingdom, Jun 5, 2021.

  1. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Hey folks

    I'm currently having some trouble with the execution order of OnTriggerEnter2D, the situation is as follows:
    • I have script which can reflect bullets, if any enters the shield radius.
    • The problem is that sometimes the OnTriggerEnter in the bullet triggers, before the OnTriggerEnter on my reflect script. This results in the bullets exploding before they can be reflected.
    • I don't think the ScriptExecutionOrder has an impact on the OnTrigger callstack.
    • I could make my BulletsExplode() function (which is called by OnTriggerEnter on the bullet) into a Coroutine and wait a frame before letting the bullet explode.
      But that delays all my explosions and that doesn't feel right.
    Any thoughts on that?

    Thanks in advance :)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Sounds like you just need to re-organize your logic, by having either the player decide what happens to the bullets on impact in all situations or having the bullets test to see if they're hitting a shield and reflect themselves.
     
    Darkkingdom likes this.