Search Unity

UnityEvent can't select from TWO of the same component

Discussion in 'Scripting' started by Vectrex, Aug 24, 2016.

  1. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    If I've got a normal UnityEvent
    eg
    public UnityEvent OnFinished = new UnityEvent();

    and I want to run a function in the inspector, it works fine..
    eg I've got this Zoomer animator that fires an event when it finishes.
    upload_2016-8-24_13-0-53.png

    BUT, if I want to run a function in ANOTHER Zoomer, eg to chain effects together, it doesn't let me select it.
    upload_2016-8-24_13-3-5.png

    The only way to do this setup is to use separate GameObjects, which is cumbersome. Is this missing? Is there a reason not to show both in the pulldown?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I'm uncertain of how UnityEvent works internally through the inspector- it may AddListener on all possible listeners matching the function signature (using GetComponents). My gut says it probably just references the first script it finds, through a single GetComponent, to add the most likely listener to the list.

    That said, you can add the necessary pieces to your own script and add the listeners manually in Start, or you can just put in a middle-man function in the Zoomer script- say named "Trigger". When Trigger() gets called on one Zoomer script, it can then use GetComponents to reference all Zoomer scripts on the same GameObject, and call Activate() on all of them.

    There may be additional benefits to having children objects that each have their own Zoomer script though- almost nothing in Unity is really set up to accommodate multiple copies of the same components on a single GameObject, and this may not be the last time you run into problems trying to do this even if you manage a workaround here.
     
    Last edited: Aug 24, 2016
    JoeStrout likes this.
  3. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    Yep, I'm guessing it just looks for the first one. But this would be trivial for Unity to fix. It seems arbitrary that Unity don't 'like' multiple copies of components. If they ever get round to drag-rearranging and renaming of components it would be much more consistent and usable.
    I usually have middle men connection scripts using C# events, but for quick effects chains the inspector and UnityEvent would be more convenient, which I believe is the point of UnityEvent over C# events.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Try drag-and-dropping the zoomer script into the object slot on the UnityEvent. I believe it will select the correct one, though I'm not completely sure.
     
    Bunny83 and LeftyRighty like this.
  5. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    111
    Sorry to bump this, but I'm still curious about this (guessing it's just how it works)
    I'm using Playmaker and it's not uncommon that you several FSM's on the same object (like you would with scripts). Problem is that each FSM is the same component type.
     
  6. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    Any news?
     
  7. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    That is not working!
     
  8. lucspinto

    lucspinto

    Joined:
    Apr 2, 2018
    Posts:
    1
    I have the same problem. Two conversation components for my dialogue system in the same Game object and the events couldn't distinguish them. Seems like it is not a reliable idea to have multiple same components and Unity Events.
    In my case I can create an empty GameObject as a children of my NPC for example for each conversation, unfortunately this is the way for my problem.
     
  9. Dima_Timofeenko

    Dima_Timofeenko

    Joined:
    Aug 4, 2015
    Posts:
    2
    This is resolved by the following steps:

    1. Drag&Drop right component (not whole gameObject) to unityEvent.Target.
    upload_2022-1-26_0-21-30.png

    2. Do not select Function in inspector! It will reset your selected component to first avaliable component in gameObject by this type.
    upload_2022-1-26_0-20-55.png

    3. Switch inspector to Debug mode and write `Method name` field
    upload_2022-1-26_0-20-31.png

    4. If your method has argument change field `Mode`.
    5. Switch inspector back to normal mode and fill arguments if your changed `Mode` in step 4.

    This works but has drawbacks. When you come back to this after a while, you will have no idea who is invoke whom...
     
  10. Digital_Mimesis_Lab

    Digital_Mimesis_Lab

    Joined:
    Mar 9, 2022
    Posts:
    24
    I have a rather easy way to fix this, you just create a class like "Zoomer1" that just inherits from the Zoomer and do nothing else. There could be 2,3,4,5 as you wish
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Interesting!

    I ran into this myself and came to the conclusion that Unity was doing a GetComponent<...> and just storing the type of the target component. If this works then that can't be the case, and that would surely mean that changing the target when setting the function name is a bug.

    Did you report it, by any chance?

    Also, from memory, Timeline has exactly this issue as well (possibly because it uses UnityEvents, but I can't remember).