Home>

C# component attached to an unspecified number of enemy objects (EnemyA, EnemyB, ....)EnemyControllerI want to execute the methods of at the same time.

C# component that manages the game systemSystemControllerNow, let's execute the process of defeating all enemy objects.

GameObject.FindGameObjectsWithTagIt is possible to get all the enemies usingInstead of the system looking,The enemy senses the system andDie()DoI want to

public class SystemController: MonoBehaviour
{
    // run this at some point
    void KillAllEnemies()
    {
        /* Kill all enemies on the scene here
        Execute Die() of EnemyController */
    }
}
public class EnemyController: MonoBehaviour
{
    public void Die()
    {
        //Detect that KillAllEnemies() of SystemController was executed and execute Die().
    }
}

Someone please tell me a function that can detect a function. Please.

  • Answer # 1

    Maybe the event handler is closer to the desired behavior.

    If you use a format like Observer, you can make it independent of each other.
    There's a dedicated guy who manages event notifications, the Enemy side subscribes for incoming calls and the SystemController side sends notifications via it.
    Image description
    * Method to notify multiple GameObjects at once by Unity Notification, quoted from EventBus

    reference
    [Unity] When is the Event Handler, Sender, or Receiver related to Event?
    https://www.hanachiru-blog.com/entry/2019/05/09/194400

    How to notify multiple GameObjects in Unity at once Notification, EventBus