GUI.TextField not working??

Discussion in 'UnityGUI' started by missy_pooh, Jul 3, 2011.

  1. missy_pooh

    missy_pooh

    New Member

    Joined:
    Jun 1, 2011
    Messages:
    150
    Hi,

    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);

    }
  2. missy_pooh

    missy_pooh

    New Member

    Joined:
    Jun 1, 2011
    Messages:
    150
    I found what wrong with the code. I should declare my variable outside the function. I need help for the second problem :)

    Thank you
  3. appels

    appels

    Member

    Joined:
    Jun 25, 2010
    Messages:
    2,666
    Last edited: Jul 3, 2011
  4. missy_pooh

    missy_pooh

    New Member

    Joined:
    Jun 1, 2011
    Messages:
    150
    Hi appels,

    thank you for the reply. But i still don't understand how to get about doing it.
  5. slide

    slide

    New Member

    Joined:
    Feb 2, 2011
    Messages:
    72
    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'"

    stringToEdit //textfield variable
    nameEdit=false //boolean variable

    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>();
    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
  6. missy_pooh

    missy_pooh

    New Member

    Joined:
    Jun 1, 2011
    Messages:
    150
    Hi slide,

    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>();
    gameDataScript.playerName=stringToEdit;//changes he value stored in the game data script for players name
    }

    And for the script, where should i put to?
  7. slide

    slide

    New Member

    Joined:
    Feb 2, 2011
    Messages:
    72
    this is the script you wrote:

    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 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
  8. missy_pooh

    missy_pooh

    New Member

    Joined:
    Jun 1, 2011
    Messages:
    150
    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 :)