Search Unity

Read and Whrite to InpudField UI

Discussion in 'Scripting' started by ki_ha1984, Sep 15, 2014.

  1. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Hi,

    I am new to Unity and i need help.

    I create some Texts and InputFields with UI of Unity 4.6 Beta 17 and i would like to ask you how i can manage them with csharp. I haven't found any script tutorial about that.

    Especially i would like to read the content of the InputField to a string and load it to a text by clicking a button?

    Any tutorial or example ?

    Thank you in advance

    Ki_ha
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    You use get component to get the text field than you can read and write its .text variable.

    Will show you later when back at my desk and have more than a phone to type on.
     
  3. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Thank you very much.

    I am waiting an example.

    Thank you again

    Ki_ha
     
  4. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    ok so if your are getting and setting the information from a other script you can do this
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class tfTest : MonoBehaviour {
    6.     public InputField myText;
    7.  
    8.     string GetText()
    9.     {
    10.         string text = myText.text.text;
    11.         Debug.Log(text);
    12.         return text;
    13.     }
    14.  
    15.     void SetText(string inputText)
    16.     {
    17.         myText.text.text = inputText;
    18.     }
    19. }
    20.  
    if the script is applied to the same object as the InputField instead of using a public InputField you would use
    Code (CSharp):
    1. gameObject.GetComponent<InputField>();
    to get the reference to the Input field component and use the same commands to get and set the information.


    be sure at the top of your CS file you are using UnityEngine.UI; or else you want have access to the InputField class in your script.
     
  5. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    It works very well, thank you cmcpasseby.

    Now one more question. I am using the "First Person Controller " of unity and i try to make a pause.
    I have the following code. My problem is that when i pres the Ecs key the "Mouse Look" component does not pause. Is still enabled.

    Any idea ?

    Code (CSharp):
    1. ublic bool paused = false;
    2.     public GameObject camPositionObject;
    3.     public GameObject playerPositionObject;
    4.  
    5.     void Update () {
    6.      
    7.         if(Input.GetKeyDown(KeyCode.Escape)){
    8.             if(!paused){
    9.                 Time.timeScale = 0;
    10.                 camPositionObject.GetComponent<MouseLook>().enabled = false;
    11.                 playerPositionObject.GetComponent<MouseLook>().enabled = false;
    12.                 paused=true;        
    13.             }else{            
    14.                 Time.timeScale = 1;
    15.                 camPositionObject.GetComponent<MouseLook>().enabled = true;
    16.                 playerPositionObject.GetComponent<MouseLook>().enabled = true;
    17.                 paused=false;
    18.             }
    19.         }
    20.     }
    21.  
    Thank you again
     
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    you could toss a if statemeant around the effected code in the mouse look component, and set that its condtion true or false in the OnEnable() and the OnDisable() mehtods.