0

cant pass parameter to the listener

[System.Serializable]
     public class EventGameObject : UnityEvent<GameObject> { }

[CreateAssetMenu]
     public class GameObjectEvent : ScriptableObject
     {
         private readonly List<GameObjectEventListener> eventListeners =
             new List<GameObjectEventListener>();

     public void Raise(GameObject go)
     {
         for (int i = eventListeners.Count - 1; i >= 0; i--)
             eventListeners[i].OnEventRaised(go);
     }

     public void RegisterListener(GameObjectEventListener listener)
     {
         if (!eventListeners.Contains(listener))
             eventListeners.Add(listener);
     }

     public void UnregisterListener(GameObjectEventListener listener)
     {
         if (eventListeners.Contains(listener))
             eventListeners.Remove(listener);
     }
 }

 public class GameObjectEventListener : MonoBehaviour
 {
     [Tooltip("Event to register with.")]
     public GameObjectEvent Event;

     [Tooltip("Response to invoke when Event is raised.")]
     public EventGameObject Response;

     private void OnEnable()
     {
         Event.RegisterListener(this);
     }

     private void OnDisable()
     {
         Event.UnregisterListener(this);
     }

     public void OnEventRaised(GameObject go)
     {
         Response.Invoke(go);
     }
 }
 >>>>gameObject X<<<<
 public class AIController : MonoBehaviour
 {
     public EventGameObject OnAIDeath;
     private void Start()
     {
     OnAIDeath.Invoke(gameObject);
     }
 }

 >>>>gameObject Y<<<<
 public class SceneMapController : MonoBehaviour
 {
     public void OnAIDeath(GameObject entityGO)
     {
         Debug.Log(entityGO);
     }
 }

GameObjectEventListener is on a SceneManager gameobject , when I drag the Scriptableobject Event on the event slot i want to use as listener SceneMapController script and run method OnAiDeath using the gameobject parameter from AIController(which is a script on another gameobject) invoke(gameObject) (check image)

Short explanation: when gameObject X dies with AIController script, i want to pass the gameObject(which died) to a listener from gameObject Y and use the gameObject received in a script function.

What I'm doing wrong?I get this error please help Unsupported type EventGameObject https://i.sstatic.net/8btOv.jpg Most code is from here: https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=2325s

1 Answer 1

1

are you trying to hook an event from the editor? if so, be careful of the receiver dropdown.

difference between dynamic and static event binding

you can see there'll be 'Dynamic bool' and 'Static Parameters', both have your receiver method with the same name. if you choose the top one, parameter will be passed from your code at runtime, but if you choose the bottom one, the parameter will always be the one you specified here: a static event binding with specified parameter

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.