1

I have a component attached to a Gameobject that contains an event with a Gameobject as parameter:

[System.Serializable]
public class ReleasedOnDifferent : UnityEvent<GameObject>
{
}
releasedOnDifferent.Invoke(this.gameObject);

the event is linked to a simple function as follow:

 public void releasedOnDifferent(GameObject gameObject)
 {
 }

from ispector

but when the event is invoked this happens:

ArgumentException: Object of type 'UnityEngine.Object' cannot be converted to type 'UnityEngine.GameObject'.

it seems that when Invoke(this.gameObject) is called a Object type is passed instead of Gameobject

Am I missing something?

0

1 Answer 1

4

UnityEvent can have two types of callbacks:

  • Static: Static calls are preconfigured calls, with preconfigured values that are set in the UI . This means that when the callback is invoked, the target function is invoked with the argument that has been entered into the UI.

By UI they here mean the Inspector.

  • Dynamic: Dynamic calls are invoked using an argument that is sent from code, and this is bound to the type of UnityEvent that is being invoked. The UI filters the callbacks and only shows the dynamic calls that are valid for the UnityEvent.

You selected the static method version and are passing in None which basically is a null placeholder and probably the reason why it can't be converted to a valid GameObject.

You rather need to select the dynamic version of the method from the upper section of the pop-up menu in order to get the dynamic parameter!

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

1 Comment

thank you! this is solved, changed from static to dynamic and it worked!

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.