1

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?

enter image description here

1
  • 1
    What is the definition of ISceneComponent? I guess it doesnt have a gameObject-field? You could add a getGameObject() method on the interface and use that? Is there any case where sender is not a gameobject? Commented Aug 15, 2018 at 21:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.