2

I'm using Unity 4.6.9, coding in c#, and all I'm trying to do is change what starts as an input field with an Integer content type and a character limit of 2, to one that has a Standard content type and a larger character limit. I've created the object itself using Unity itself, so I can't figure out how to change it in the script. What i'm trying to do is have a single field be responsible for 2 integers and a string input, and i have the entirety of the code completely working, EXCEPT for changing the type and limit while the program is running. I've been googling and trying things for almost 2 hours now, so I'm hoping I didn't just miss something. I've already tried looking through Unity's documentation and that hasn't helped me what-so-ever.

For the purpose of answering this, my Input Field is named InputField, the class it's in is named UIInputField, and the actual output from that field is named tempInput (the data that gets sent from that field into the program). And like I said, I'm just trying to change "Content Type" and "Character Limit", I've already got the code worked out to convert strings to ints, as well as a failsafe if the input field is left blank, or has the wrong character type.

if I've left anything out that is necessary for this question to be answerable, let me know.

EDIT: Sorry, I left out the code thinking it wouldn't be necessary (thought this would be an easy fix that I just hadn't figured out yet) So here's the code for what I've got so far (I do know that there are portions of this program that could create an issue depending on what the user puts in, and already know how to fix it, but I'm trying to see about getting this input issue working properly before I worry about those issues):

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

public class UIInputField : MonoBehaviour {
    string[] particNames;
    string[] particInit;
    int subState = 1;
    int state = 1;
    int particVar;
    // Update is called once per frame
    void Update () {

    }

    //Fix substate handling before building.
    public void getInt(string tempInput){
        if (state == 1) {
            if (Int32.TryParse (tempInput, out particVar))
                Debug.Log (particVar);
            particNames = new string[particVar];
            particInit = new string[particVar];
            GameObject.Find ("InputField").characterLimit = 12;
            GameObject.Find ("InputField").ContentType = standard;
            state++;
        } else if (state == 2) {
            particNames [subState] = particVar;
            subState++;
            GameObject.Find ("InputField").characterLimit = 2;
            GameObject.Find ("InputField").ContentType = integerNumber;
            if (subState == particNames.Length){
                state++;
                subState = 1;
            }
        } else if (state == 3) {
            if (Int32.TryParse (tempInput, out particVar))
                Debug.Log (particVar);
            particInit[subState] = particVar;
            subState++;
            if (subState == particNames.Length) /* The code to move on the the next portion of this program will be here once this portion is working properly*/;

        }
    }
}
2
  • Please describe your problem with valid code
    – Rakin
    Nov 12, 2015 at 4:27
  • Sorry, I assumed that this issue would be simple enough to not need it, but I suppose I should always bring it up, huh? I added the code into the OP. Nov 15, 2015 at 2:13

1 Answer 1

5

After doing more searching over the past couple days, I found the answer to this question. It uses:

mainInputField.contentType = InputField.ContentType.Standard;
mainInputField.characterLimit = 12;//Or any integer

mainInputField is just a variable at this point, of type InputField, and you have to go into the UnityEditor to set that variable to reference the actual input field variable.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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