Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Korpsian · Sep 17, 2017 at 12:08 PM · buttoneventsystemcalling

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 ^^

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Sep 17, 2017 at 01:12 PM 0
Share

How far does the code execution get?

avatar image Korpsian hexagonius · Sep 17, 2017 at 01:26 PM 0
Share

It get's to the DoIt() $$anonymous$$ethod and prints the Debug.Log("I'm doing it"!); but ignores the invoke

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!! :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Korpsian · Sep 18, 2017 at 05:27 PM 0
Share

ha yeah you'r right ^^ sadly this is not helping with the problem :/

avatar image Vubb3 · Sep 18, 2017 at 05:30 PM 1
Share

guess that's all I got sadly.

avatar image Vubb3 · Sep 18, 2017 at 05:47 PM 1
Share

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 ;)

avatar image Korpsian Vubb3 · Sep 18, 2017 at 05:50 PM 0
Share

its just a piece of code, not everything, just to get the problem straight.

avatar image Vubb3 Korpsian · Sep 18, 2017 at 05:52 PM 1
Share

Guess what i wanna do is to check in a update method if the button is triggered, if so execute the action.

avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Korpsian · Sep 18, 2017 at 04:16 PM 0
Share

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

avatar image tormentoarmagedoom Korpsian · Sep 18, 2017 at 04:30 PM 0
Share

$$anonymous$$mm you should try to Invoke all methods 1 by 1. How are you writing the invoke code?

obj.GetComponent

avatar image Korpsian tormentoarmagedoom · Sep 18, 2017 at 05:09 PM 0
Share

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 :/

Show more comments
avatar image hexagonius Korpsian · Sep 18, 2017 at 06:28 PM 0
Share

Yes you can just invoke any UnityEvent. The problem seems to be that nothing is registered to the event that gets invoked

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unusual trouble with OnPointerClick 1 Answer

Can I use EventSystem + InputModule to implement scroll via button? 0 Answers

UI button not working 1 Answer

Changing the target of a button that takes a gameobject as a parameter? 0 Answers

How do you call a function with a button? Unity 5 UI 8 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges