I’m a bit green when it comes to this…
I’ve been trying to use UnityEvents to pass information between classes.
this page of the API leads me to believe that this is, in fact, possible: Unity - Scripting API: UnityEvent<T0>
when i tried doing this almost exactly, it didn’t work, so i’ve been wrestling with it, but so far i’ve mostly been running into walls. here’s code that follows that example almost exactly:
public UnityEvent ChooseInteractionEvent;
// Use this for initialization
void Start()
{
if (ChooseInteractionEvent == null)
ChooseInteractionEvent = new UnityEvent();
//listen for interaction event
ChooseInteractionEvent.AddListener(ActivateWheel);
}
public void ActivateWheel(InteractableObject target)
{
ActiveObject = target.gameObject;
}
similar to what is shown in the API, ActivateWheel is a method in this class which takes 1 argument, my goal is to activate it from another class.
The line where I add the listener is gave me the error "Argument 1: Cannot convert from ‘method group’ to UnityAction"
the research i did has been confusing as to what exactly the difference is and what the consequences of changing ActivateWheel to a unity action are.
the parameter, the InteractableObject “target” is the information i’m trying to pass between classes, so I can’t get rid of it. I want to invoke the event from another class so I can pass that information to this class.
When the Event is invoked, the only argument i gave it is the target object, that line is also throwing an error.
this.gameObject.GetComponent<localPlayer>().ChooseInteractionEvent.Invoke(hit.transform.gameObject.GetComponent<InteractableObject>());
that’s the line where i’m invoking the event. but my attempt to invoke it throws the error that the “invoke” method can’t take any arguments.
so, is it not possible to pass parameters through events? does it only work if it’s within the same class? Do i need to use delegates to make events work between classes even with UnityEvents?
In case that method is moot, i tried about 3 other ways of doing it, so I may need to ask some followup questions about why those methods are also failing.
it’s also possible that this is not how events with parameters work, since when i add to the UnityEvent, it seems to be okay, until i actually run the program, then it doesn’t work