c# - How to change the text of a button on clicking it in UNITY? -


enter image description herei want change text of button on clicking in unity. new c#. kindly me out!

the script added button element

using unityengine; using system.collections; using unityengine.ui;  public class buttontextchange : monobehaviour {  text buy;       // use initialization     void start () {      buy = transform.findchild("text").getcomponent<text>();     }      // update called once per frame     void update () {      }  public void clicked() { debug.log("button buy clicked!"); buy.text = "i button!";  } } 

i have tried lot of answers not working out me! have button inside canvas. appreciated ! enter image description here

your problem comes setup of text , buttons.

the buttontextchange component on buttontext object. can see on picture object has no child. run:

void start () {     buy = transform.findchild("text").getcomponent<text>(); } 

which getting error. text object under button object pass in onclick call.

public class buttontextchange : monobehaviour  {     public void clicked(text textref)     {         debug.log("button buy clicked!");         textref.text = "i button!";     } } 

then in inspector drag text object parameter slot.

edit: user wants change button color on click:

public class buttontextchange : monobehaviour  {     public void clicked(gameobject buttonobj)     {         debug.log("button buy clicked!");         text text = buttonobj.getcomponentinchildren<text>(true);         if(text != null){             textref.text = "i button!";´         }         image image = buttonobj.getcomponent<image>();         if(image != null){             image.color = newcolor;          }     } } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -