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 /
This question was closed Oct 08, 2015 at 02:51 AM by Xeong-Hu for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Xeong-Hu · Oct 07, 2015 at 01:18 PM · listarraysfindindex

How do you find an Index of an Arrayed/Listed Object?

For example I have 5 Marbles and I want to find the 3rd Marble in my group.

Pretty Simple.

All I want to do is find out which Index is this GameObject in my group.

Such as.

 public List<GameObject> Marbles;
 
 print(Marbles[2].index);


But i'm pretty sure it isn't that simple haha.

If anyone can help me i'd appreciate it.

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Garazbolg · Oct 07, 2015 at 02:25 PM

You can simply use the IndexOf() of List : https://msdn.microsoft.com/fr-fr/library/e4w08k17(v=vs.110).aspx

public int IndexOf( item);

In you case your case (if i understood well) you have a list and a gameObject and you want to now the index of this gameObject in the List.

public List Marbles; int index = Marbles.IndexOf(Marbles[2]) //index will be equal to 2

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 Xeong-Hu · Oct 07, 2015 at 04:48 PM 0
Share

Thank you both for providing that link. I was honestly having a hard time understanding the IndexOf rule from the original site.

Anyways.

It worked just as perfect as ever. Now my Cards move from the position depending on their order position and what not, its great. Thanks Guys!

avatar image
2

Answer by DiegoSLTS · Oct 07, 2015 at 02:21 PM

I don't get it, isn't the index "2"? You're just writing it there.

When you do "Marbles[2]" you get a GameObject reference, and GameObject doesn't have a member called "index".

If you already have an object reference and want to know the index where it can be found in a list there's the "IndexOf" method: https://msdn.microsoft.com/en-us/library/e4w08k17%28v=vs.110%29.aspx

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 Xeong-Hu · Oct 07, 2015 at 02:38 PM 0
Share

Thanks for the reply!

$$anonymous$$hm yes that is true, but I am using a for loop function In my script.

Which Does this.

             for(int i = 1; i < NumberOfCards; i++){
                 print(DeckCards[i].gameObject.name);
 }


I want to be able to get the Card's order number, by getting it's index.

And then move it's position and other few things based on it's card order.

But i'll check out your link and see how it turns out.

Follow this Question

Answers Answers and Comments

31 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

Related Questions

Searching a List of GameObjects by name 2 Answers

A node in a childnode? 1 Answer

Unity c# indexoutofrangeexception: array index is out of range 1 Answer

How to return index of List element? 1 Answer

How to find a Class by value 2 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