Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Is it possible to use more than one Event Manager for Unity Event?

Discussion in 'Scripting' started by ClaudiaKrog, Aug 31, 2020.

  1. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    45
    Hello,

    I am using Unity events to call specific actions based on which object is selected. I have 5 objects that can be selected - each one has an OVRGrabbable script on it, in which I am putting the event (which is called when the object is grabbed). When the object is grabbed, a different event happens in the environment.

    Is it possible to essentially have 5 eventmanager scripts (the OVRGrabbable script on the 5 objects), which calls an event, and executes something different based on which object the event is called within.

    Is it possible to pass the current gameobject in the event as well?

    I am wondering if there is a better way to do this
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,740
    I'm not sure why you'd need multiple Event Manager's for this.

    You have distinct OVRGrabbable components on each of the 5 things. So each can have a distinct event on them (what sort of event are you using? UnityEvent, C# Event, what?). So you should be able to just handle each one individually.

    So if say you use UnityEvent on your OVRGrabbable, for each of the 5 things, have their distinct UnityEvent target a distinct target.

    Also if you want to include the sender along with it, then create a UnityEvent<TypeOfSender> instead using the UnityEvent<T> type, and pass it along when you invoke it:
    https://docs.unity3d.com/ScriptReference/Events.UnityEvent_1.html

    ...

    If you could, share your code to help us better understand your current implementation.
     
  3. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    45
    I actually have the same OVRGrabbable script on each of the objects (one script - this is from Oculus for VR development). I think the problem is that there are 5 distinct objects, but only one script which will send a general "grabbed" event. But, there will be 5 different actions that happen based on which object is sending the event
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,781
    A C# class is like a template for creating objects following a particular pattern, and the individual objects created are called instances of that class. If you attach the same script to 5 different Unity GameObjects, they're all the same class, but they're separate instances, which means they can have unique data stored in each one (including a reference to which object they're attached to).

    If you want to treat events differently depending on which of the 5 objects emitted them, then those events should have some kind of parameter that identifies the source. This could be a reference to the actual component or gameobject, or some kind of identifying name or code.
     
  5. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    45
    Yes, thank you, that all makes sense. I am wondering how exactly I could pass the grabbed object in the Unity Event using a parameter
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,740
    I already linked to how you can send parameters along with the UnityEvent. You would create a UnityEvent that inherits from UnityEvent<T>, where T is the type of the object you're going to be passing a reference to. Then when you call invoke, you just pass a ref to that object. Basically how the example in the link works, but instead of <int>, you're going to be <MyScriptType>.

    Furthermore, this is likely not necessary. Even though it's all the same script/class, each instance is distinct/unique. Which is what Antistone is trying to say to you. So each object that has your OVRGrabbable script on it with a UnityEvent. You just configure it to target the specific target you want. Each instance targeting a different target.

    ...

    As I stated before, we can better assist you if you share your code.
     
unityunity