Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Input Field returning a blank.

Discussion in 'Editor & General Support' started by RagingWolf1109, Mar 8, 2024.

  1. RagingWolf1109

    RagingWolf1109

    Joined:
    Jan 21, 2024
    Posts:
    4
    Hi, I am using the following code to , alongside other things, store the value of an input field to a variable. However when i print the variable it returns a blank. Any help would be much appreciated.

    I am using the On End Edit in the input field to trigger the GetFuelEfficiency and I do see under the input field inspector in the TextMeshPro-Input Field under text that text does appear in the spot.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    using UnityEngine.UI;
    using TMPro;

    public class AGVNavScript : MonoBehaviour
    {
    GameObject pickUp;
    GameObject dropOff;
    GameObject charger;
    GameObject Editable_Text_Box;
    int state;
    Vector3 destination;
    private NavMeshAgent agent;
    bool travel = false;


    public float fuelMax;
    public float fuelCurrent;
    public float efficiency = 1000;
    Vector3 lastPosition;
    bool toPickup = true;

    //Needed for Edit Fuel Efficiency and Max Fuel
    public TextMeshProUGUI Cur_Max;
    public TextMeshProUGUI Cur_Efficiency;
    public TMP_InputField fuelEfficiencyInputField;
    public TMP_InputField maxFuelInputField;
    private string fuelEfficiency;
    private string maxFuel;


    void Start()
    {
    agent = GetComponent<NavMeshAgent>();
    agent.updateRotation = false;
    agent.updateUpAxis = false;
    state = 0;

    pickUp = transform.parent.GetChild(0).gameObject;
    dropOff = transform.parent.GetChild(1).gameObject;
    charger = transform.parent.GetChild(2).gameObject;

    // Game Objects for Fuel Efficiency and Max Fuel Editings
    Editable_Text_Box = GameObject.Find("Editable_Text_Box");
    Cur_Max = GameObject.Find("Cur_Max").GetComponent<TextMeshProUGUI>();
    Cur_Efficiency = GameObject.Find("Cur_Efficiency").GetComponent<TextMeshProUGUI>();
    fuelEfficiencyInputField = GameObject.Find("Fuel_Efficiency").GetComponent<TMP_InputField>();
    maxFuelInputField = GameObject.Find("Max_Fuel").GetComponent<TMP_InputField>();


    lastPosition = transform.position;
    fuelCurrent = fuelMax;

    }

    public void Travel()
    {
    travel = !travel;
    }

    void setDestination()
    {
    switch (state)
    {
    case 0: destination = pickUp.transform.position; break;
    case 1: destination = dropOff.transform.position; break;
    case 2: destination = charger.transform.position; break;
    default: destination = transform.position; break;
    }
    }

    void Update()
    {
    if (travel)
    {
    if (fuelCurrent <= 0)
    {
    state = 8;
    }
    else if (fuelCurrent < 10)
    {
    state = 2;
    }
    setDestination();
    agent.destination = destination;

    fuelCurrent -= Vector3.Distance(transform.position, lastPosition) / efficiency;
    }
    else
    {
    agent.destination = transform.position;
    }

    //Changing the text of Cur_Max and Cur_Efficiency to the current values of fuelMax and efficiency
    Cur_Max.text = fuelMax.ToString();
    Cur_Efficiency.text = efficiency.ToString();
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
    if (collider.tag == "Charger")
    {
    fuelCurrent = fuelMax;
    if (toPickup)
    {
    state = 0;
    }
    else
    {
    state = 1;
    }
    }
    else if (collider.tag == "SimSet")
    {
    if (toPickup)
    {
    state = 1;
    }
    else
    {
    state = 0;
    }
    toPickup = !toPickup;
    }
    }

    //string s = xField.GetComponent<InputField>().text;
    public void GetFuelEfficiency(string s)
    {
    Debug.Log(s);
    fuelEfficiency = s;
    Debug.Log(fuelEfficiency);
    }


    public void ok_ButtonClicked()
    {
    //Debug.Log(GameObject.Find("Editable_Text_Box").transform.GetChild(6).transform.position.x);
    Debug.Log(fuelEfficiency);
    // Debug.Log("Fuel Efficiency: " + fuelEfficiencyInputField.text);
    // Debug.Log("Max Fuel: " + maxFuelInputField);


    //If the text is not empty, the values the input fields are set to be the new values of the fuel efficiency and max fuel
    // if (fuelEfficiencyInputField.text != "")
    // {
    // efficiency = float.Parse(fuelEfficiencyInputField.text);
    // }
    // if (maxFuelInputField.text != "")
    // {
    // fuelMax = float.Parse(maxFuelInputField.text);
    // }

    }


    }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,885
    please use code tags

    don‘t use gameobject.find(„“), use inspector references

    prefer UI Toolkit over UGUI

    don‘t call Parse but TryParse, Parse will throw an exception for non-float values
    also parsing float from string is riddled with locale issues … what‘s the decimal separator, comma or dot? You have to specific the parameter that makes Parse use both (can‘t recall from memory, check the c# reference)
     
  3. RagingWolf1109

    RagingWolf1109

    Joined:
    Jan 21, 2024
    Posts:
    4

    I am unsure what you mean by Inspector References can you clarify? I am not super worried about Parse throwing an error as the a number is being entered as a string.
     
  4. Ruslank100

    Ruslank100

    Joined:
    Apr 11, 2018
    Posts:
    46
    Inspector reference is a way to reference (link or assign) an object/component from the editor. You make a public variable (for example the input field) in the script, then it will appear as a field you can assign an input field into, inside the editor. It's much less error prone than GameObject.Find(), which will fail if you mistyped the name, changed the object name in the editor, or if you have more than one object with the same name (it won't necessarily find the one you needed!)
    Regarding parsing - you don't want to underestimate the user. Never assume the user will type in a legitimate input. What if typed fast and I inserted "1009\" into the field? Parse() will fail and throw an exception, because of the '\', TryParse() on the other hand will just return false (failed to parse) on this one, so you can handle the case in the code