Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by WroughtINT · Sep 20, 2016 at 04:57 PM · c#arraycomparestrings

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WroughtINT · Sep 21, 2016 at 09:57 AM 0
Share

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

avatar image Namey5 WroughtINT · Sep 21, 2016 at 10:19 AM 0
Share
  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.

avatar image WroughtINT Namey5 · Sep 21, 2016 at 12:26 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to have 2 references in an array? 1 Answer

Destroy a Prefab from an Array? (C#) 2 Answers

Getting the closest GameObject to the player from an array 1 Answer

How to get all children of a Gameobject with a certain component 2 Answers

Storing input to array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges