Unity Community


Results 1 to 8 of 8

  1. Posts
    150

    GUI.TextField not working??

    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. Posts
    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. Posts
    2,603
    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/i...itle=Singleton
    Last edited by appels; 07-03-2011 at 02:03 AM.


  4. Posts
    150
    Hi appels,

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


  5. Posts
    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<GameDataScrip t>();
    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. Posts
    150
    Quote Originally Posted by slide View Post
    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<GameDataScrip t>();
    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
    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<GameDataScrip t>();
    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. Posts
    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. Posts
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •