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 /
avatar image
0
Question by Tides · Jul 06, 2011 at 10:56 PM · arraypositionfindelement

How do I find the position of an element in an array?

Hey guys.

I'm having a bit of trouble with finding the position of a string in an array using JScript. Whats the best way to go about this?

I know what the strings are but I need to know what the positions of them are.

Cheers.

Comment
Add comment · Show 5
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 Graham-Dunnett ♦♦ · Jul 07, 2011 at 10:42 AM 0
Share

This is a Javascript question, not a Unity one.

avatar image Romano · Feb 01, 2014 at 03:16 PM 0
Share

The question seems relevant here because javascript or a variation of it is a coding language used in Unity. Not to mention that Unity seems to have its own take on arrays.

avatar image Eric5h5 · Feb 01, 2014 at 05:29 PM 0
Share

Unity uses .NET/$$anonymous$$ono arrays, nothing unique. (Aside from the Array class, which is an implementation that's the same as web Javascript arrays.)

avatar image Romano · Feb 01, 2014 at 06:45 PM 0
Share

Thanks for clearing that up. $$anonymous$$y main point there was that the question isn't irrelevant as the answer will help other unity users.

avatar image Eric5h5 · Feb 01, 2014 at 11:23 PM 0
Share

The problem is that you could say that about all general program$$anonymous$$g questions, since Unity uses program$$anonymous$$g. But general questions would be better served elsewhere since they aren't specific to Unity; that way we can focus on Unity-only questions here.

3 Replies

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

Answer by dibonaj · Jul 07, 2011 at 02:29 AM

 for(int i = 0; i < array.Length; i++)
 {
    Debug.Log("The element in index " + i + " is " + array[i];
 }

I think that should about do it.

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
2

Answer by flaviusxvii · Jul 06, 2011 at 11:04 PM

Loop through the array.

Comment
Add comment · Show 3 · 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 Tides · Jul 06, 2011 at 11:06 PM 0
Share

Thats what I'm doing, looping through the array to find the string I want but I need to know the index of that string.

avatar image Eric5h5 · Jul 07, 2011 at 12:29 AM 0
Share

@Tides: if you're already looping, then you have the index. That's what the loop index tells you.

avatar image Dreamblur · Jul 07, 2011 at 02:41 AM 0
Share

You can also use the FindIndex and/or IndexOf methods of the Array class (.NET arrays, not UnityScript arrays), but those methods are specialized, so manually looping through your array is generally faster.

avatar image
1

Answer by DrPunchman · Aug 10, 2018 at 09:27 PM

Here is my example, hope this helps someone: It's a rough snippet of something I'm working on.

     public string[][] pass = new string[2][];
     
     // define the array somewhere like:
             pass[0] = new string[] { "ant", "bug" };
             pass[1] = new string[] { "dog", "boy" };
     
     // check for what's in the list
  void checkPassword(string k)
 {
             int i = 0;
             int l = pass[0].Lenth; 
             
             for(i=0; i < l; i++) // don't define vars in your for() it's slow
             {
                 if(pass[0][i] == k) {  print("index --> " + i);  }
             }
 }

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

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

8 People are following this question.

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

Related Questions

How do you find one Vector's position of an Array? 1 Answer

How do I instantiate same tag/name GameObjects with an array 1 Answer

how subtract total value element of array ?? 1 Answer

Swapping elements of an array 1 Answer

How to only instantiate objects once? 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