I am wondering how come my GUI.TextField are not working properly?? I manage to display it. But when i enter some value into the textField, there is nothing display on the screen?? What wrong with my code??
And to add on, for example, if player enter their name,the player expect to see a “Welcome (Mickey->enter by player)” on the next scene. But if player did not enter any name and click continue, the next scene will show “Welcome Player”. How do i go about doing it??
Thank you
function OnGUI () {
var stringToEdit : String =“Your name”;
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField(Rect(320, 230, 230, 25), stringToEdit, 25);
You can create a singleton class to save things and retrieve them in another scene. Or you could just make a variable static and grab it from another scene.
There are some examples on the wiki : http://www.unifycommunity.com/wiki/index.php?title=Singleton
It seems you need to bring your stringToEdit variable into the next scene once the player has changed it. to do this create an empty game object and write a script with the DontDestroyOnLoad () function in the start method so that it is not destroyed when a new scene loads.
You would also need the GUI.changed() function to check if the player entered a new value in the textfield. Ok I code in C# so there could be errors in this script but it should work.
In the gamedata game object you need a string variable in the script playerName=“Welcome” if the player changes the name in the textfield then it will change value of the variable so that instead of your welcome screen saying “Welcome ‘player’” it would say “Welcome ‘slide’”
OnGUI(){
stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25);//your code to create the textfield here
if (GUI.changed)
{
nameEdit=true;
}
if(GUI.Button(new Rect(Screen.width/2-ButtonWidth/2,buttonyPosition,ButtonWidth,ButtonHeight),“Start”))//your startbutton here
{
GameObject gameData=GameObject.Find(“GameData”);//find the game data object
if(nameEdit==true){//checks if the the name was edited
GameDataScript gameDataScript=gameData.GetComponent();
gameDataScript.playerName=stringToEdit;//changes he value stored in the game data script for players name
}
This is probably a bit buggy but should work any problems let me know
Thank you for the reply. But it seem very complicated to me.
if(GUI.Button(new Rect(Screen.width/2-ButtonWidth/2,buttonyPosition,ButtonWidth,ButtonHeight),“Start”))//your startbutton here
{
GameObject gameData=GameObject.Find(“GameData”);//find the game data object
if(nameEdit==true){//checks if the the name was edited
GameDataScript gameDataScript=gameData.GetComponent();
gameDataScript.playerName=stringToEdit;//changes he value stored in the game data script for players name
}
var stringToEdit : String =“Your name”;
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField(Rect(320, 230, 230, 25), stringToEdit, 25);
}
you want to add what i suggested to it. basically a button for the player to press when they have wrote their name and can move to the next scene. the stringToEdit variable is updated in the game data script.
do you have any scripts at the moment? if you can send it to me i’ll try update it
yes. Basically i have a textcontrol.js and playercontrol.js.
The textcontrol.js is attached to the guiText(Continue) on the scene.
For the playercontrol.js, it will be attached to the scene when i want the playername to be display. → i not sure i have to attached the script to which object. Anyway i will send you a PM. check out your inbox