- Home /
Creating a component from a button click
Hello everyone. I've stuck in one error.. When a click in a button i want to create a component, something like a Label, or a text field. When i click in the button nothing happens. I already did the: Debug.Log("You clicked in a button"); Works fine, but nothing is created. I've tried this:
public Texture2D[] acordingTxt = new Texture2D[10];
void OnGUI() {
if(createButton(50, 50, 100, 40, buttonTxt[0])) {createLabelC(550, 400, 500, 500, acordingTxt[0]);
GUI.Label(new Rect(550, 400, 500, 50), acordingTxt[0]);
Debug.Log("you've created a label");
}
}
the createLabelC() and the createButton() is my methods to make what GUI.Button and GUI.Label, i've tried create this Label from GUI.Button(...) but nothing happens too! Just the Debug.Log(""); appears to work.
Thank you guys.
Answer by robertbu · May 05, 2013 at 06:35 AM
GUI.Button() returns true only for a single frame (when the button is released). So your label is being shown for 1/60 of a second or so. Instead of GUI.Button(), use GUI.RepeatButton(). It will return true as long as the button is held down.
yea, to create an element ins$$anonymous$$d of RepeatButton because i need one click and the element "has created!".
Here is one way to do it. Attach to an empty game object and test, then you can integrate the concept into your code:
public class ClickAndRepeate : $$anonymous$$onoBehaviour {
Rect rect = new Rect(50, 50, 100, 40);
void OnGUI() {
Event e = Event.current;
if (e.type == EventType.mouseDown && rect.Contains(e.mousePosition))
Debug.Log ("Fire once per click");
if (GUI.RepeatButton(rect, "Button")) {
GUI.Label(new Rect(550, 400, 500, 50), "Button was pressed");
}
}
}
Your answer
Follow this Question
Related Questions
Null reference exception[SOLVED] 1 Answer
GUI.Label positioning for many device resolutions 1 Answer
GUI, making main page lead into an instructions page 2 Answers
GUI button doesn't appear on Android 0 Answers
Make GUI Button's size fit texture? 2 Answers