Waiting for an entity to appear/attach. Events? Coroutines?

Options
In many of my scripts -- both on entities and normal MonoBehaviours -- I wait for specific entities to appear. My current solution is a mix of coroutines that search a few times every second, and events that I trigger when an entity has been attached. Neither works well.

Is there a better way? What do you do?

Comments

  • dmg
    Options
    public override void Attached() {}
  • chr
    Options
    @dmg: An entity waits for another entity. Example: Table and Plate entities. Plate happens to attach first, so there's no table yet. Plate waits for Table to attach so it can do something with it (like move itself to the table)
  • dmg
    Options
    hmm..

    Im not exactly sure if there is a better way but maybe some kinda coroutine

    on your plate.

    while(table.isReady == false) { yield return new WaitForSeconds(0.1f); }

    then inside of your table.

    public override Attached() { isReady = true; }
  • chr
    Options
    Yup, that's close to what I'm doing at the moment. It works, but it's not very clean. Using events can in some cases make it more simple (when an entity has attached it triggers an event for anyone listening)