Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

trouble with callback-methods of UI-elements on Instantiating via script

Discussion in 'UGUI & TextMesh Pro' started by starface606, Feb 4, 2015.

  1. starface606

    starface606

    Joined:
    Sep 5, 2014
    Posts:
    7
    Hello Community,
    I have the following problem: i want to instantiate a UI-prefab, which has some predefined Callback-methods.
    imagine a prefab with a UI-Button with an Onclick() method. the moment i instantiate the prefab in runtime, the method call in the UI-Button is empty... do i have to assign the method calls while instantiating or how is it done else?

    thank you in advance! if somethings unclear, please be welcomed to ask:)
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    To add an event to an Object that implements one of the UnityEvents, you simply need to use the AdListener method, for example on a button you would:
    Code (CSharp):
    1. myButton.onClick.AddListener(() => Debug.Log("The Button was clicked"));
    Hope this helps. Although code attached events will not appear in the editor!
     
  3. starface606

    starface606

    Joined:
    Sep 5, 2014
    Posts:
    7
    thanks, havent tried yet, but i will try to report asap:)