Search code examples
c#unity-game-engineuser-input

Having Issue with Setting Up multiple Input Field in Unity3d


I'm currently trying to set up multiple Input Field in Unity and having issue do so. I can only get one input to work (intPressure) but don't know how to add a second one (drillpipe). It seem to me that unity only allow one Input Field at a time. I would think that just changing it to InputField2, ect will do the trick, but that doesn't seem to work. Other have mentioned creating an empty gameobject and inserting the inputfield to each gameobject.

To be honest, I still quite confuse about how to setup the second input field.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

    public class UserInput : MonoBehaviour {

        InputField input;

        int intPressure = 0;
        int drillpipe = 0;

        public DataManager data;

        void Start ()    
        {
            var input = gameObject.GetComponent<InputField>();
            var se= new InputField.SubmitEvent();
            se.AddListener(SubmitName);
            input.onEndEdit = se;
        }

        private void SubmitName(string pressure)    
        {

            var pressureScript = 
              GameObject.FindObjectOfType(typeof(DataManager)) as DataManager;

            if (int.TryParse(pressure, out intPressure))
            {
                pressureScript.pressure = intPressure;
            }
            else
            {
                // Parse fail, show an error or something
            }
        }
   }

Solution

  • I am not sure about the way you want to implement this (using single GameObject), however you most certainly would be able to do this if you had a couple of GameObjects (One object per control) nested within another GameObject (let's call it UserInputRoot) like this:

      UserInputRoot (has UserInputController script)
      |
      +--- InputPressure (has InputField component)
      |
      +--- InputDrillpipe (has InputField component)
    

    The controlling script would then have either a couple of public InputField's or a couple of private ones, initialized within the Start() or Awake() method:

    class UserInputController : MonoBehaviour {
        //These can be set from the inspector and controlled from within the script
        public InputField PressureInput;  
        public InputField DrillpipeInput; 
    
        // It would be better to pass this reference through
        // the inspector instead of searching for it every time
        // an input changes
        public DataManager dataManager;
    
        private void Start() {
            // There is no actual need in creating those SubmitEvent objects.
            // You can attach event handlers to existing events without a problem.
            PressureInput.onEndEdit.AddListener(SubmitPressureInput);
            DrillpipeInput.onEndEdit.AddListener(SubmitDrillpipeInput);
        }
    
        private void SubmitDrillpipeInput(string input) {
            int result;
            if (int.TryParse(input, out result)) {
                dataManager.drillpipe = result;
            }
        }
    
        private void SubmitPressureInput(string input) {
            int result;
            if (int.TryParse(input, out result)) {
                dataManager.pressure = result;
            }
        }
    }
    

    And by the way, the formatting of your code is absolutely atrocious. You MUST fix it.