I have an event manager in Unity which can handle events sent from scene components. The classes are all a bit abstracted from the standard Unity components. I am successfully able to fire an event from a button, and have that button's parent listen to the event (the onEventReceived
you see below).
Using the Unity Debugger extension for VSCode, I can log the sender. If I type sender.gameObject
and it returns a correct reference to the GameObject the sender is attached to. However, I cannot reference sender.gameObject
within the OnEventReceived
function itself - because it is of type ISceneComponent
I suppose. How do I get a reference to the game object of the clicked sender?
I have run into your issue myself before.
The fix is quite simple.
Assuming that you created the interface ISceneComponent, add the gameObject field to your Interface
public interface ISceneComponent
{
GameObject gameObject { get; set; }
// ...
}
If the object implementing ISceneComponent also is a monobehaviour, gameObject will automatically be filled and you can reference it easily.