Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Gathering

Discussion in 'Scripting' started by Geo141994, Feb 6, 2017.

  1. Geo141994

    Geo141994

    Joined:
    Feb 6, 2017
    Posts:
    4
    Hi all .... I already have about 3 hours when I bother.
    How to gather two inputsField in c # unity 2d.

    public InputField field1;
    public InputField field2;
    //public GameObject Response_great;
    int total_response;
    int field_text1 = 4;
    int field_text2 = 4;

    // Use this for initialization
    void Start () {

    field_text1 = int.Parse(field1.text);
    field_text2 = int.Parse(field2.text);
    total_response = field_text1 + field_text2;

    }

    // Update is called once per frame
    void Update () {
    }
    public void Check()
    {
    Debug.Log("Total:" + total_response);
    }


    It's the code .... and the answer is 0. Why and how to correct? Please help....tanks!!!
     
  2. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Try the following. Instead of:
    field_text1 = int.Parse(field1.text);
    field_text2 = int.Parse(field2.text);

    Have:
    Debug.Assert(int.TryParse(field1.text, out field_text1));
    Debug.Assert(int.TryParse(field1.text, out field_text1));

    This will throw an error if there was something wrong with the string and it couldn't get a int from it.
     
  3. Geo141994

    Geo141994

    Joined:
    Feb 6, 2017
    Posts:
    4
    If you can tell me in general how to gather two inputs.....and how to screen out the answer.Thank you!!
     
  4. VengeanceDesign

    VengeanceDesign

    Joined:
    Sep 7, 2014
    Posts:
    84
    Putting the information on the screen is easy. Just place a Text for reach one and say value1Text.text = "Value1: "+value1;

    Getting the info depends where the info comes from. If it comes from another script you just say value1 = script.value;

    If the player must set the value you'd need to find a tutorial for that.