Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UnityEvent and Invokes

Discussion in 'UGUI & TextMesh Pro' started by Avoozl, Mar 20, 2015.

  1. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Hi

    I know that the UnityEvent.Invoke function invokes an event instance. But is there an easy way to invoke ALL instances of a UnityEvent in the scene? Here is my event class:

    Code (CSharp):
    1. [System.Serializable]
    2. public class EventCameraChange : UnityEvent{}
    The event is then added to objects via components (possibly of different types) with the member
    Code (CSharp):
    1. public EventCameraChange CameraEvent;
    This event is supposed to be 'scene wide' in that any number of game objects must know when it happens, not just one.

    Thanks in advance.
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    If I understand you correctly, you don't want to use the inspector to make the event known to all objects (which can get pretty tedious), but rather want to do it by scripting only. I think that should be done by using the Singleton pattern.
     
  3. Avoozl

    Avoozl

    Joined:
    Jan 13, 2012
    Posts:
    36
    Hi blizzy,

    Thanks for the reply. You understand me correctly. I could use the Singleton Pattern. But I was wondering if Unity offered a native method, since it already offers Invoke for instances. EG:

    MyEventType.InvokeAll();
     
  4. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    I'd recommend using Advanced C# Messenger or something similar, since it lets you fire off events in a decoupled way. For example I use a modified version that fires off an event when the screen resolution changes. Anything that needs to be resized can opt into the message and change it's size as needed.