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\$
2
  • \$\begingroup\$ You can check System.Environment.NewLine; \$\endgroup\$ May 24, 2016 at 13:56
  • \$\begingroup\$ How to do that though? \$\endgroup\$ May 24, 2016 at 20:13

1 Answer 1

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

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .