C# Comparing a String to an Array?
I have an Array of strings. What I have done Is set up a command prompt in unity. When I press enter I want my script to see if the entered text is the same as any in my string array, and if it is, execute the appropriate command.
For example; If I type "/home" I want to press enter and for my script to find "/home" in my array, and then I want it to execute the appropriate command. (Which is just changing the scene)
Thank you!
Answer by Namey5 · Sep 20, 2016 at 09:28 PM
I would personally do it through a foreach loop, i.e.
public int GetScene (string[] array, string input)
{
foreach (string s in array)
if (input == s)
return System.Array.IndexOf (array, s);
return null;
}
This function takes an array and an input string, and returns the index of the input string in the array if there is a match.
That is exactly what I want but I am having trouble integrating it. Sorry, but it has been a very long time since I last coded and I was still rusty at best. Here is my script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Command_Input : $$anonymous$$onoBehaviour {
public Text Command_Text;
public InputField Actual_Text;
public $$anonymous$$eyCode Confirm;
public string[] Commands;
public GameObject History_Window;
public Text History_Text;
public float Notice_Timer_Seconds;
public float Start_Timer_Seconds;
public bool Has_Set;
// Use this for initialization
void Start()
{
Command_Text = GetComponent<UnityEngine.UI.Text>();
History_Text = History_Window.gameObject.GetComponent<UnityEngine.UI.Text>();
}
// Update is called once per frame
void Update()
{
if (Notice_Timer_Seconds >= 0)
{
Notice_Timer_Seconds -= Time.fixedDeltaTime; //Reduce Timer
Actual_Text.text = ">>Inputting Command<<"; //Digital Display, Confir$$anonymous$$g Input
}
else
{
if (!Has_Set)
{
Actual_Text.text = ""; //Set Input field to blank
Has_Set = true;
}
}
// Actual_Text = Command_Text.text;
if (Input.Get$$anonymous$$eyDown(Confirm))
{
Debug.Log("Entered");
//Compare Strings Here
History_Text.text = "User/: " + Actual_Text.text + "\n" + History_Text.text; //Add Current Input text to the History Log
Notice_Timer_Seconds = Start_Timer_Seconds; //Start Timer
Has_Set = false;
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Command_Input : $$anonymous$$onoBehaviour {
public Text Command_Text;
public InputField Actual_Text;
public $$anonymous$$eyCode Confirm;
public string[] Commands;
public GameObject History_Window;
public Text History_Text;
public float Notice_Timer_Seconds;
public float Start_Timer_Seconds;
public bool Has_Set;
// Use this for initialization
void Start()
{
Command_Text = GetComponent<UnityEngine.UI.Text>();
History_Text = History_Window.gameObject.GetComponent<UnityEngine.UI.Text>();
}
public int GetCommand (string[] array, string input)
{
foreach (string s in array)
if (input == s)
return System.Array.IndexOf (array, s);
return null;
}
// Update is called once per frame
void Update()
{
if (Notice_Timer_Seconds >= 0)
{
Notice_Timer_Seconds -= Time.fixedDeltaTime; //Reduce Timer
Actual_Text.text = ">>Inputting Command<<"; //Digital Display, Confir$$anonymous$$g Input
}
else
{
if (!Has_Set)
{
Actual_Text.text = ""; //Set Input field to blank
Has_Set = true;
}
}
// Actual_Text = Command_Text.text;
if (Input.Get$$anonymous$$eyDown(Confirm))
{
Debug.Log("Entered");
int command = GetCommand (Commands, Actual_Text.text); //Compare Strings Here
if (command)
{
switch (command)
{
case 0:
//Command 1
break;
case 1:
//Command 2
break;
case 3:
//And so on...
}
}
else
{
Debug.Log ("Command does not exist.");
}
History_Text.text = "User/: " + Actual_Text.text + "\n" + History_Text.text; //Add Current Input text to the History Log
Notice_Timer_Seconds = Start_Timer_Seconds; //Start Timer
Has_Set = false;
}
}
}
Something like that. Not quite knowing how you're doing it there's not much more I can do. What I personally would do is make 'Commands' an array of a serializable class rather than just a string array, because then you can have multiple elements, i.e. a second string variable as a function to invoke, etc.
This is looking good. Thank you for all of your help, it's been great. But I have two last errors, it seems:
It is saying that GetCommand doesn't exist in the context. And that it cannot convert an Int to a bool where it says "switch (Command)".
And I certainly do need to learn more about serializable classes, I'm still trying to grasp the basics as I'm not great at scripting.