- Home /
when button pressed, textfield appears
hey guys, i'm a newbie in Unity, and i'm working on an usability Platform.
I think i have an easy problem,.... but not easy enough for me...
i just want when someone press my button, a textfield appears!
How can i solve this problem? :(
Thanks a lot!! Tina :)
Answer by SkaredCreations · Nov 23, 2011 at 05:10 PM
Here is:
public class TestScript : MonoBehaviour {
private bool showForm = false;
private string myText = "";
void OnGUI()
{
if (!showForm)
{
if (GUI.Button(new Rect(0, 0, 100, 20), "Show Form"))
showForm = true;
}
else
{
myText = GUI.TextField(new Rect(0, 0, 100, 20), myText);
}
}
}
looks great, but how can i use this script? is it c#? and how can i use it on my camera? what is monobehavior??
Thank youuuu Tina :)
Yes it is C#, $$anonymous$$onoBehaviour is the base class of every attachable script component (http://bit.ly/rYPA9u), since this script is only displaying stuff on GUI you can attach it to whatever object, even the camera
Assets/Buttons.cs(1,27): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?
Why is this? What do i have to do?
What does it mean "everytime"? You have to attach it only once in the hierarchy of a scene (of course before you hit "PLAY" button). If you mean that you need to use it in more than one scene, than you could create a prefab in Project and drag there the camera so you should use this prefab as your camera in every scene ins$$anonymous$$d of the default one.
Answer by cj_coimbra · Nov 23, 2011 at 04:48 PM
private bool textFieldActive = false;
void OnGUI() { if (textFieldActive) { //place your textfield code here }
if( GUI.Button(buttonRect, "Activate TextField") )
{
if (textFieldActive)
textFieldActive = false;
else
textFieldActive = true;
}
}
Answer by Tinawchen · Nov 24, 2011 at 09:32 PM
hello ZioRed! What is monobehavior? and how can i use your script? is it c#? and how do i have to bind it to my camera?
Tina :)
Your answer
Follow this Question
Related Questions
Problem with GUI.Button and other GUI items 1 Answer
Gui label and gui button 1 Answer
GUI Scripting button controls 1 Answer