Search Unity

Add new listener to already instantiated gameobject

Discussion in 'Scripting' started by multimediamarkers, Sep 1, 2020.

  1. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
    Hi there,

    I have a component called "PopUpManager". It subscribes to several events of other components that are instantiated in the first that is loaded.

    Like: InternetConnectionController.Instance.OnInternetConnectionNotWorking += InternetConnectionNotWorking;

    But in following scenes other gameobjects with components are instantiated like "LibraryController". How can i add the listener "LibraryController.OnLibraryLoaded" also to the already instantiated gameobject/component PopUpManager?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The easiest way is probably to just make the event static, so that it can be subscribed to anywhere at any time and doesn't depend on any instances of an object existing.
     
    gaglabs likes this.
  3. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    49
    @Vryken Maybe that is the best approach! Thanks!