Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by legion_44 · Feb 08, 2013 at 03:56 PM · guiarrayconsolestrings

Checking that this String is in this Array

Hi, i want to make something like cheat console using text field and array of cheats strings. And when i type for example player.kill in text field unity will search if this string is in Cheats
Strings Array and make an Label "Succeed" when it is and "Failed" when it's not.

Thanks in advance and sorry for my English.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Eric5h5 · Feb 09, 2013 at 08:38 PM

You don't need to loop through arrays or anything, just use IndexOf, which returns -1 if the item isn't found.

 var strings = ["yo", "blah", "foo"];
 if (System.Array.IndexOf (strings, "blah") != -1) {
     Debug.Log ("Yep.");
 }

Or better yet, assuming you're using a recent version of Unity, Array.Contains:

 var strings = ["yo", "blah", "foo"];
 if (strings.Contains ("blah")) {
     Debug.Log ("Yep.");
 }
Comment
Add comment · Show 1 · 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 legion_44 · Feb 10, 2013 at 08:41 AM 0
Share

Thank you a lot :) This is the best method!

avatar image
0

Answer by OWiz · Feb 08, 2013 at 04:03 PM

So make your array of strings and loop through them whilst comparing them to what the user typed. You can compare them with "`==`"

Comment
Add comment · 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
0

Answer by Kiloblargh · Feb 08, 2013 at 08:25 PM

It's pretty straigtforward and trivial to translate what you wrote into a functioning script if you know anything about scripting at all.

Just off the top of my head:

    var cheatStrings : String[];
 
    var testString : String = consoleDisplay.text;
    var isValid : System.Boolean = false;
    for (var s : int = 0; s < cheatStrings.Length; s++)
         {
         if (testString == cheatStrings[s])
             {
             isValid = true;
             }
         }
      if (isValid)
          {
          Debug.Log "Succeed";
          }
      else
          {
          Debug.Log "Failed";
          }

So, if something like that didn't just come to you when you thought about the problem, you will have a lot more difficulties ahead of you and before taking on the game you want to make, you need to read and reread the Unity scripting reference, as well as freshen up on programming in general.

Comment
Add comment · Show 6 · 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 legion_44 · Feb 09, 2013 at 11:36 AM 0
Share

But, is this will working fast when i will have about 3000 strings?

avatar image OWiz · Feb 09, 2013 at 06:58 PM -1
Share

Try it and see. The general rule is "Don't worry about performance unless you have to".

avatar image Shiner252 OWiz · Nov 22, 2017 at 07:27 PM 1
Share

This is not true

avatar image ShadyProductions OWiz · Nov 22, 2017 at 07:33 PM 1
Share

I don't know where u got that 'general rule' from but thats so not true. LOL. If I said that at the company I develop for they would laugh at me.

avatar image LoungeKatt OWiz · Sep 11, 2018 at 02:42 PM 0
Share

The general rule is not to let performance dictate functionality unless you have to. Developers should never play whisper down the alley with general rules.

avatar image Nikola7007 · Sep 11, 2018 at 02:50 PM 0
Share

This is a late response, but a HashSet might work better than an array. I haven't tested it though, and I'm not sure if the performance impact is significant either way (since this comparison only happens when a console command is run, I assume)

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

15 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

Related Questions

Where is the directory for GUI skin 0 Answers

Trying to set text equal to a gameobject array value but it's including (unityengine.gameobject) in the text 1 Answer

Use gui slider to change text field and vice versa? 1 Answer

How to show an array of custom objects in Inspector? 2 Answers

GameObject Array in Editor GUI 3 Answers


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