Search Unity

Question Sending signals to objects not in scene (global signals?)

Discussion in 'Timeline' started by DKaras, Dec 1, 2021.

  1. DKaras

    DKaras

    Joined:
    Sep 5, 2015
    Posts:
    5
    Is it possible to send (emit) signals from timeline to object(s) that you are not able to reference during creation of timeline?

    People in all the examples I've seen, simply just drag and drop an object with receiver onto emitter in the signal track. But what if I want to send a signal to an object created/instantiated during the runtime?

    The only hacky solution I came up with is to send the signal to some kind of proxy-object that exists in a scene, which holds references to these dynamically created other objects and "propagates" the signal to them.

    Are there any better/easier/less hacky way to do this?
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Timelines can only hold references to objects that are part of the same scene as the PlayableDirector. So are UnityEvents. If you want a notification system that doesn't have these limitations, you will need to implement one and link it with the Signal.

    That said, what some people have done is use a ScriptableObject as the target of Signals, instead of components. These ScriptableObjects then trigger custom notification solutions, which can include global events.

    You can then reuse the same ScriptableObject asset in multiple Timelines, without necessarily having to store that ScriptableObject in the scene.
     
    DKaras likes this.
  3. DKaras

    DKaras

    Joined:
    Sep 5, 2015
    Posts:
    5
    Thank you very much. I wouldn't have thought of using ScriptableObjects this way. I tested it right now in a simple "sandboxy" project and it works fine. I even managed to inject some dependencies with Zenject into this ScriptableObjecct with Container.QueueForInject. Which makes this approach really elegant and "proper" - from lack of better words.
     
    DavidGeoffroy likes this.