0

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
            }
        }
   }

2 Answers 2

2

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.

9
  • To be honest, I'm actually an animator trying to learn programming. So it taking me awhile to really make sense of it.
    – Coder
    Commented Jan 12, 2016 at 14:17
  • Use IDE like mono-develop to format your code, try to use spaces instead of tabs so that your code looks allright when you copy-paste somewhere (Configure your IDE to use 4 spaces instead of a tab). Since you use C#, it would be best to follow Microsoft's coding standarts: msdn.microsoft.com/en-us/library/ff926074.aspx or at least something more or less consistent.
    – MarengoHue
    Commented Jan 12, 2016 at 14:19
  • I know it sloppy but my main focus to get the code to work correctly first, and then work on formatting at a later time.
    – Coder
    Commented Jan 12, 2016 at 14:23
  • As of now, I actually have two gameobjects. Each gameobject has a field input.
    – Coder
    Commented Jan 12, 2016 at 14:25
  • Now you have to write a script which will sit on a root gameobject (see the tree diagram) and use InputFields on those gameobjects.
    – MarengoHue
    Commented Jan 12, 2016 at 14:26
0

It looks like the script you are showing is attached to an input field gameobject is that correct? You have a member variable in your object named input but then you create a new variable input in your Start method. Rather than have a script attached to your first InputField, create an empty gameobject in the editor and attach a script to that. In the script add two public members:

 public class UserInput : MonoBehaviour {

  public InputField PressureInput;
  public InputField DrillPipeInput;

Now pop back to the editor, you should see the two input fields showing up when you select your empty gameobject. Drag and drop your two input fields into the slots for each inputfield. Now when the scene starts your UserInput script will be set with the input fields and you can use them both.

1
  • Ok, let me look into it.
    – Coder
    Commented Jan 12, 2016 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.