• 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
Question by IMTRIGGERHAPPY9 · Oct 10, 2011 at 04:21 AM · gameobjectarrayobjectindex

finding an objects index in a multi dimensional static array?

ok so i have a selector that tells me the name of my object in my multi-dimensional array, but i want to know how i can have it send me back the index of that object. can anybody help me out?

Comment

People who like this

0 Show 2
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 syclamoth · Oct 10, 2011 at 04:33 AM 0
Share

Depends on how your selector works! Presumably, to get the name of the object, you had to have the index at some point anyway- so return that instead!

I think you can use IndexOf(object), but that might only work for generic lists (I can't remember...)

avatar image IMTRIGGERHAPPY9 · Oct 10, 2011 at 04:35 AM 0
Share

my selector is using raycasting

1 Reply

  • Sort: 
avatar image

Answer by syclamoth · Oct 10, 2011 at 04:44 AM

Given that you know the transform (I assume it is an array of transforms?) you can use

 var xPos : int = -1;
 var yPos : int = -1;

 for(var i : int = 0; i < array.GetLength(0); i++)
 {
     for(var j : int = 0; j < array.GetLength(0); j++
     {
          if(selectedTransform == array[i, j];
          {
              xPos = i;
              yPos = j;
              break;
          }
     }
     if(xPos != -1)
     {
          break;
     }
 }

And then you can use xPos and yPos! If they return -1, it means the selected transform isn't in the grid (oops);

Comment

People who like this

0 Show 7 · 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 IMTRIGGERHAPPY9 · Oct 10, 2011 at 04:49 AM 0
Share

why do you have that break there?

avatar image syclamoth · Oct 10, 2011 at 04:53 AM 1
Share

break tells it "Ok I'm done with this now, don't bother looping through the rest of the iterations. kthxbai!". I've got it there to save time, although you don't techincally need it.

avatar image IMTRIGGERHAPPY9 · Oct 10, 2011 at 04:56 AM 0
Share

if i where to turn this into a function i would have it return the xPos and the yPos right? and then i would send it my array that i have? thats it?

avatar image IMTRIGGERHAPPY9 · Oct 10, 2011 at 05:00 AM 0
Share

thanks dude if i could give you a million credit points i would right now! you have been so helpful!

avatar image syclamoth · Oct 10, 2011 at 05:01 AM 0
Share

Kind of? You can't return two values at a time without defining a type which contains them, and you'd need to send it both the array, and the transform you're searching for. I'm really not sure what the context of what you're doing is... (well obviously it's your TD that you are making, but more specifically)

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Removing objects from an array 2 Answers

GameObject turns into Object in Array 1 Answer

How to remove objects from a list ? 3 Answers

Need a Custom array Index to look up gameobject tags? 2 Answers

Swapping elements of an 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