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 primusSpenat · Apr 24, 2017 at 03:23 PM · arraysindex

Finding specific index of an array

I just cant seem to solve this little problem. I know that there is a lot of documentation on this subject, but none of those seems to help me.

Just to simplify the problem, lets say i have a few Buttons (unitys UI stuff) which all are part of an array. When i click one of the buttons, i want to know what that certain buttons index number is from the array. From what i have found out, alot of people say that you could use System.Array.IndexOf. But i dont see how that could help me, or in other words, i dont get it to work. Does anyone have any ideas?

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 alankemp · Apr 24, 2017 at 04:12 PM

If we have an array on integers with some content:

 int[] numbers = { 15, 42, 3, 126, 999 };

And we wanted to know what index the number 42 occured at, we could write a loop to find it like this:

             int indexOf42 = -1;
 
             for (int i = 0; i < numbers.Length; i++)
             {
                 if (numbers[i] == 42)
                 {
                     indexOf42 = i;
                     break;
                 }
             }
 
             Console.WriteLine("indexOf42={0}", indexOf42);

Remembering that indicies into an array are 0 based, this will print "indexOf42=1".

You can use Array.IndexOf to do the same work. To find the index of 999 in the array:

 int indexOf999 = Array.IndexOf(numbers, 999);
 
 Console.WriteLine("indexOf999={0}", indexOf999);
Comment
Add comment · Show 4 · 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 primusSpenat · Apr 24, 2017 at 05:08 PM 0
Share

yes, that part i have got the hang of. The problem is that i do not know how to get the certain index of the button i pressed. Just for an example. I have these 5 buttons, and when i press the second button from left it has the index number of 1. In other words, i know what index number every button have when i look at them. Just, how do i tell the script which index number it have?

i can throw in my script here which assigns characters to different buttons.

public class Button$$anonymous$$anagerCharacters : $$anonymous$$onoBehaviour {

     public GameObject[] buttonList;
     public GameObject[] partyList;
 
     private void Update(){
         AddCharToButton();
     }
 
     // This is where i assign both the Name and the Sprite of the Character.
     public void AddCharToButton() {
         string[] characterNames = System.Enum.GetNames(typeof(Character$$anonymous$$anager.CharacterNames));
 
         // Character Names
         for (int i = 0; i < buttonList.Length; i++) {
             if (i < characterNames.Length - 1)
                 buttonList[i].GetComponentInChildren<Text>().text = characterNames[i + 1];
             else
                 buttonList[i].GetComponentInChildren<Text>().text = characterNames[0];
         }
 
         // Character Sprites
         for (int i = 0; i < buttonList.Length; i++)
         {
             if (i < characterNames.Length - 1)
                 buttonList[i].GetComponent<Image>().sprite = Resources.Load("Characters/" +
                                                                             characterNames[i + 1],
                                                                             typeof(Sprite)) as Sprite;
             else
                 buttonList[i].GetComponent<Image>().sprite = Resources.Load("Characters/" +
                                                                             characterNames[0],
                                                                             typeof(Sprite)) as Sprite;
         }
     }
 
     // This is where i want to know the index number of the certain button i pressed!
     public void AddCharToParty() {
         
     }
 
 }
avatar image primusSpenat primusSpenat · Apr 24, 2017 at 05:10 PM 0
Share

Wow, sorry for how hard this script is to read.

avatar image alankemp primusSpenat · Apr 24, 2017 at 05:44 PM 1
Share

I usually create a small component, say called "CharacterButton" in this case, and add it to the button game object, something like this:

 public class CharacterButton : $$anonymous$$onoBehaviour
 {
     public int Index;
 
     public void Init(int index)
     {
         Index = index;
         // code from AddCharToButton could be moved here
     }
 }

Then when you create the button use AddComponent() to add this class, and call .Init(< the index>) to initialise it.

Later you can use GetComponent() and then check .Index for its index in the array.

avatar image primusSpenat alankemp · Apr 25, 2017 at 08:26 AM 0
Share

Yeah, this actually seems like a nice workaround! Unity should add something like Array.FindIndexNumber(); (or something similiar) just to get around all this. But what do i learn from being lazy, huh? ;)

Thank you for the help my good sir!

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

101 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

Related Questions

How can I find the index of an object in an array from that object's script? 0 Answers

Parameter Name: Index 0 Answers

C# array error index is zero but when i try to assign it to variable it suddenly change to 3 0 Answers

C# ArrayList Accessing and RemoveAt? 0 Answers

Another Physics.OverlapSphere question 0 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