Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Edit Prefab button event via script.

Discussion in 'UGUI & TextMesh Pro' started by UndeadLeech, Aug 22, 2014.

  1. UndeadLeech

    UndeadLeech

    Joined:
    Jan 26, 2014
    Posts:
    3
    Hello,

    I tried using a button for my game as prefab. Importing it and placing it wasn't a very big problem, but when you Instantiate the button it has no function which will be called. So now I'm asking myself how I am able to set up the Button function outside of the Editor by script?

    Hope you can help and understand me.

    Greetings
    UndeadLeech
     
  2. rzubek

    rzubek

    Joined:
    Aug 21, 2014
    Posts:
    72
    Assuming you have a gameObject that has a Button component on it, you can do this (in C#):

    Button buttonComponent = gameObject.GetComponent<Button>();
    buttonComponent.onClick.AddListener(yourClickHandlerFunction);
     
  3. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    can do it with an anonymous click handler too,

    buttonComponent.onClick.AddListener(() =>
    {
    _fsm.State = new ActionInputState();
    });