1
\$\begingroup\$

I need help with input fields. I already set the line type to 'Multi_Line NewLine' in inspector, but I have problem with submitting the string. Here's the code:

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

public class GameController : MonoBehaviour {
    public string code;
    public string code1;
    [SerializeField]
    public InputField input;

    public void Check(string guess)
    {
        if (string.Equals (guess,code)) {
            Debug.Log ("correct");
        } else
            Debug.Log ("wrong");
    }
    public void GetInput(string guess)
    {
        Check (guess);
        input.text = "";

    }
}

I want to have both 'code' and 'code1' strings to be inserted in different lines inside the same input field but I don't know how to check both 'code' and 'code1' string value inside the Check(). Any help would be appreciated.

\$\endgroup\$
  • \$\begingroup\$ You can check System.Environment.NewLine; \$\endgroup\$ – Paweł Marecki May 24 '16 at 13:56
  • \$\begingroup\$ How to do that though? \$\endgroup\$ – user3132527 May 24 '16 at 20:13
0
\$\begingroup\$

I'm assuming guess variable will have a text from InputField, if so you could use this line to split the string from checking to NewLine

string[] guessWords = guess.Split('\n');

and then retrieving the specific word from the guessWords array like

if (string.Equals(guessWords[0], code))
{
    Debug.Log("code 1 correct");
}
else
{
    Debug.Log("code 1 wrong");
}
if (string.Equals(guessWords[1], code1))
{
    Debug.Log("code 2 correct");
}
else
{
    Debug.Log("code 2 wrong");
}

note you may still need to handle IndexOutOfRangeException if you only input one line to InputField

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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