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();
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();
Cur_Efficiency = GameObject.Find(“Cur_Efficiency”).GetComponent();
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().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);
// }
}
}