Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Dec 16, 2016 at 08:58 PM by GrKl for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by GrKl · Dec 16, 2016 at 12:40 PM · uiruntimebutton trigger eventslistenerevent-listener

Button AddListener during runtime pointing to another GameObject?

Hi all,

I'm trying to create buttons during runtime and point 'OnClick' event to function on a component on another GameObject.

Simple view of my bit of code:

 int yPos = -60;
 Active active = hit.transform.GetComponentInParent<Active>();
 for (int i = 0; i < active.allowedCraft.Length; i++)
 {
     GameObject newBut = Instantiate(Resources.Load("Button"), new Vector3(0,0,0), Quaternion.identity) as GameObject;
     newBut.transform.parent = machineTipGroup.transform; //machineTipGroup is a UI panel with a canvas group component
     newBut.transform.localPosition = new Vector3(0,yPos,0);
     yPos +=30;
     newBut.GetComponentInChildren<Text>().text = active.allowedCraft[i].ToString();
     newBut.GetComponent<Button>().onClick.AddListener(() => active.ChangeRecipe());
 }


No error is thrown by Unity, but the buttons created do not contain any OnClick event. 'hit.transform.gameObject' is not the object on wich this script is attached to.

'ChangeRecipe()' is public and present in component attached to hitted transform GO.

First time adding buttons during runtime, searched on UA and others but did not find a working solution.

Thanks for your help and time

EDIT: code is clearly not fine-tuned for performance. Was just a 'make it work first THEN make it work nicely' approach

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by Fluffy_Kaeloky · Dec 16, 2016 at 12:53 PM

I just tested it with Unity 5.4.1 with a similar piece of code :

     public GameObject prefab = null;
 
     public void Start()
     {
         GameObject instance = GameObject.Instantiate(prefab);
         instance.transform.SetParent(transform, false);
 
         Button butt = instance.GetComponent<Button>();
 
         butt.onClick.AddListener(() => { Debug.Log("Clicked !"); });
     }

It works just fine. For the OnClick Event in the UI not showing, that's normal. The only thing I can think of is checking your active variable and code, there must be something wrong. Also, try writing a log in your lambda, just to be certain that the event is fired.

Edit : I just realized that I have very poor choice in variable names.

Edit2: I tested it with a new piece of code :

 public class ButtonInstantiate : MonoBehaviour
 {
     public GameObject prefab = null;
 
     public OtherScript other = null;
 
     public void Start()
     {
         GameObject instance = GameObject.Instantiate(prefab);
         instance.transform.SetParent(transform, false);
 
         Button butt = instance.GetComponent<Button>();
 
         butt.onClick.AddListener(() => {
             Debug.Log("Clicked !");
             other.Foobar();
         });
     }
 }

and OtherScript being :

 public class OtherScript : MonoBehaviour
 {
     public void Foobar()
     {
         Debug.Log("Foobar has been called.");
     }
 }

And it works just fine for me. Is there something I don't understand ?

Comment
Add comment · Show 4 · 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 GrKl · Dec 16, 2016 at 01:12 PM 0
Share

Thanks @Fluffy_$$anonymous$$aeloky . But you are here adding a fixed piece of code to your button. I'm trying to add a call to a function on a specific GameObject. Just as one could do with the editor functionality of the button

avatar image Fluffy_Kaeloky · Dec 16, 2016 at 01:26 PM 0
Share

I edited my answer following your comment.

avatar image GrKl Fluffy_Kaeloky · Dec 16, 2016 at 01:40 PM 0
Share

Thanks, I did no know that it was normal that the editor was not showing the event properly. I'll test that when @home

avatar image GrKl Fluffy_Kaeloky · Dec 16, 2016 at 08:57 PM 0
Share

Ok... I feel stupid... The script originally posted worked... I just had an on mouse click event to toggle the pannel with the buttons depending on what GO the raycast hit. I thought that even if the raycast would cause the panel (and thus the buttons) to disapear would occur but that the click event on the button would still trigger. Turns out it does not...

I thus just changed the click to show the panel on the right click, leaving the left click to interact with the buttons, undisturbed by my stupidity...

Thanks again for your help!

Follow this Question

Answers Answers and Comments

90 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect if UI button is clicked? 3 Answers

UI button stops working after reload scene 1 Answer

Instance not found only when using button press to execute function,Canvas instance not found when executing function via button press 0 Answers

When using AddListener, can multiple buttons be registered to the same function? 1 Answer

UI: Execute action instead of "Navigation" 0 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