- Home /
click the button and text field appears below the button
Hey i'm making a game for my thesis now, and i need help, I'm using c# AND i have these select your character scene with button with image of character in it. But I want it to be when user clicked the button,the text field with default name will appear below the button instead of text field in the button.
please anybody help me? thank you :)
these are my current code:
using UnityEngine;
using System.Collections;
public class SelectingCharacter : MonoBehaviour {
public Texture2D texture = null;
public float guiPlacementY1;
public float guiPlacementY2;
public float guiPlacementY3;
public float guiPlacementY4;
public float guiPlacementX1;
public float guiPlacementX2;
public float guiPlacementX3;
public float guiPlacementX4;
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), "Player");
}
GUI.Box (new Rect(350,80,600,450), "");
if (GUI.Button(new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .1f, Screen.height * .2f), "Male"))
{
if (!showForm)
showForm = false;
else
showForm = true;
}
else if (GUI.Button(new Rect(Screen.width *guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .1f, Screen.height * .2f), "Female"))
{
if (!showForm)
showForm = false;
else
showForm = true;
}
else if (GUI.Button(new Rect(Screen.width *guiPlacementX3, Screen.height * guiPlacementY3, Screen.width * .1f, Screen.height * .1f), "Back"))
{
Application.LoadLevel(0);
}
else if (GUI.Button(new Rect(Screen.width *guiPlacementX4, Screen.height * guiPlacementY4, Screen.width * .1f, Screen.height * .1f), "Next"))
{
Application.LoadLevel(5);
}
}
}
Posting your current code will get you a more accurate answer. See GUI.Label().
sorry,it's my first time asking here so,how to post the code?
You can click on the 'edit' link in the bottom left corner of your question to edit your question. Copy your code from $$anonymous$$onodevelop and paste it into the edit view. Select your code and use the '101/010' button to format your code.
alright,that's my current code. thank you, so now do you know what i should use ins$$anonymous$$d of using form code?
Answer by Ibzy · Feb 03, 2014 at 12:21 PM
Hi MissD,
Firstly, I think you can replace if (!showForm) showForm = false; else showForm = true; With showForm = !showForm as you are using that if statement as a toggle - this will tidy the code up a little.
If I understand your question correctly, you would like it to say "Player" in a textfield that you can then choose to change?
I hit this snag myself and the result I got was something like:
using UnityEngine;
using System.Collections;
public class SelectingCharacter : MonoBehaviour {
public Texture2D texture = null;
public float guiPlacementY1;
public float guiPlacementY2;
public float guiPlacementY3;
public float guiPlacementY4;
public float guiPlacementX1;
public float guiPlacementX2;
public float guiPlacementX3;
public float guiPlacementX4;
private string myText = "Player";
private bool showName = false;
void OnGUI(){
GUI.Box (new Rect(350,80,600,450), "");
if (GUI.Button(new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .1f, Screen.height * .2f), "Male"))
{
showName = true;
}
if (GUI.Button(new Rect(Screen.width *guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .1f, Screen.height * .2f), "Female"))
{
showName = true;
}
if (GUI.Button(new Rect(Screen.width *guiPlacementX3, Screen.height * guiPlacementY3, Screen.width * .1f, Screen.height * .1f), "Back"))
{
Application.LoadLevel(0);
}
if (GUI.Button(new Rect(Screen.width *guiPlacementX4, Screen.height * guiPlacementY4, Screen.width * .1f, Screen.height * .1f), "Next"))
{
Application.LoadLevel(5);
}
if (showName)
{
GUI.TextField(new Rect(0,0,100,20), myText));
}
}
}
Thanks for the help! Yeah that's what I mean,so user can play with name "Player" or they can change it to whatever they want. But the text "Player" still cannot be deleted,got any idea why?