- Home /
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 :/
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 ^^
It get's to the DoIt() $$anonymous$$ethod and prints the Debug.Log("I'm doing it"!); but ignores the invoke
Answer by Vubb3 · Sep 18, 2017 at 05:23 PM
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!! :)
ha yeah you'r right ^^ sadly this is not helping with the problem :/
using UnityEngine; using UnityEngine.UI; using UnityEngine.UI.Extensions; using System.Collections.Generic; using UnityEngine.Events; public class $$anonymous$$yClass: $$anonymous$$onoBehaviour { public gameobject obj; voidStart() { button.GetComponent().onClick.AddListener(DoIt); }
void Update () { if (DoIt != null) { debub.log ("Test"); } }
I copying your script and gets lots of errors. using UnityEnginge.UI.Extensions for example.
Does this work?? Just try it if you have time. Thats all i got ;)
its just a piece of code, not everything, just to get the problem straight.
Guess what i wanna do is to check in a update method if the button is triggered, if so execute the action.
Answer by tormentoarmagedoom · Sep 18, 2017 at 07:54 AM
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 :D
sorry for not clear enough. in line 11 i talked about a IU button (sorry) and on line 16 i thought you can just call every UnityAction in the UnityEvent by just use "$$anonymous$$yUnityEvent.Invoke()".
Also by trying to use Invoke() with a method name just leads to an error.
https://docs.unity3d.com/462/Documentation/ScriptReference/Events.UnityEvent.Invoke.html
$$anonymous$$mm you should try to Invoke all methods 1 by 1. How are you writing the invoke code?
obj.GetComponent
I first get a method name with
string str = obj.GetComponent<Script>().$$anonymous$$yUnityEvent.GetPersistent$$anonymous$$ethodName(0);
and then try to invoke it with
obj.GetComponent<Script>().$$anonymous$$yUnityEvent.Invoke(str, 1f);
but i just get the error that invoke() does not take overloads. Also the documentation from Unity shows nothing about that :/
Yes you can just invoke any UnityEvent. The problem seems to be that nothing is registered to the event that gets invoked
Your answer
![](https://koobas.hobune.stream/wayback/20220612135033im_/https://answers.unity.com/themes/thub/images/avi.jpg)