Invoke other UnityEvent with gameobject.GetComponent().onClick.AddListener();

I try to invoke a other UnityEvent by pressing a button but it looks like it’s not working for some reason :confused:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
using System.Collections.Generic;
using UnityEngine.Events;

public class MyClass: MonoBehaviour {
public gameobject obj;

Start() {
button.GetComponent<Button>().onClick.AddListener(DoIt);
}

public void DoIt(){
Debug.Log("I'm doing it"!);
obj.GetComponent<Script>().MyUnityEvent.Invoke();
}

}

In “MyUnityEvent” is just another debug.log but i don’t get any response or error code.

Delegate is not an option because every object have different event’s to call.
What’s wrong here? Can somebody help? thanks ^^

Good day @Korpsian !

First, where/how you define this variable button of line 11?

button.GetComponent<Button>().onClick.AddListener(DoIt);

second, in line 16

obj.GetComponent<Script>().MyUnityEvent.Invoke();

You need to say what you want to invoke, and the time elapsed to execute (how many seconds after executing the line to execute the method you are invoking). Like…:

obj.GetComponent<Script>().MyUnityEvent.Invoke(MethodToInvoke, 2f);

Bye :smiley:

Start() {

Is the one up there.

There should be

void Start() {

Should do it right??

Maybe already fixed but couldn’t find anyone writing bout it!! :slight_smile: