Reputation: 49
I want print a value on the button when it is clicked.How it is possible?
[Update]
using UnityEngine;
using System.Collections;
public class tic : MonoBehaviour
{
void OnGUI() {
string value="";
if(GUI.Button(new Rect(10,10,50,50),value)) {
value="y";
print ("y");
GUI.Button(new Rect(10,10,50,50),value);
}
}
}
Upvotes: 1
Views: 2663
Reputation: 4260
You can do like this;
var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"
Upvotes: 0
Reputation: 4056
Adapting the Unity gui example you can modify the button text by storing that text in a variable and changing it when the button is clicked, like so:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public string ButtonText = "Click Me"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), ButtonText )) {
ButtonText = "Huzzah!";
}
}
}
The button will first read as "Click Me" then will change once to "Huzzah".
If you don't want to change the actual text in the button it gets a little tougher. You would need to create a label that sits over the button, I don't recommend going this route. It won't look nice and the label won't move w/ the button:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public bool DrawLabel = false;
public string LabelText = "Huzzah"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), "Click Me")) {
DrawLabel = true;
}
if(DrawLabel){
// use the same rect parameters as you did to create the button
GUI.Label (new Rect (10, 10, 150,100), LabelText);
}
}
}
Upvotes: 1