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 NerdRageStudios · Oct 12, 2015 at 09:15 PM · arrayfor-loopindex

Find the index of a GameObject within an array

Please can someone help me with finding the index of a gameobject in an array as I loop through it?

Basically, I have an array of Gameobjects, and I want to enable the selected weapon, and disable all the others.

I have written the following code which I assumed would work fine, but when I call this, all gameobjects simply de-activate.

Can someone see if I have done anything wrong here?

     public void ChangeWeapon(int weaponID)
     {
         int weaponToChangeTo = weaponID;
 
         for (int i = 0 ; i < weapons.Length; i ++) 
         {
             if (System.Array.IndexOf(weapons, i) != weaponToChangeTo)
             {
                 weapons[i].SetActive(false);
             }
             if (System.Array.IndexOf(weapons, i) == weaponToChangeTo)
             {
                 weapons[i].SetActive(true);
             }
         }
     }
Comment
Add comment · 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 saschandroid · Oct 13, 2015 at 07:57 AM 0
Share

The 2 answers below provide a better solution for your problem ... but to answer your question "Can someone see if I have done anything wrong here?": Yes :) The method you are using is [IndexOf(Array, Object)][1]. That means in your code you are searching for the object "i" in the array "weapons" which doesn't exist. So the returned index is (generally) always -1 and always != weaponToChangeTo.

[1]: https://msdn.microsoft.com/en-us/library/7eddebat%28v=vs.110%29.aspx

avatar image NerdRageStudios saschandroid · Oct 13, 2015 at 09:06 AM 0
Share

Thanks for that, I was struggling to understand why my code wasnt working, thats very helpful :)

2 Replies

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

Answer by CharlieMcF · Oct 12, 2015 at 11:32 PM

I think you might be overthinking this problem.

You're passing the Index of the weapon you want to change to. This means you want that weapon to be active, and all others to be inactive.

Rather than doing what you are currently doing, I would simply loop through all of the weapons in your array, and set them to be Inactive. Once this loop is complete, simply set the weapon at your desired index to be active.

Eg:

  public void ChangeWeapon(int weaponID)
      {
  
          for (int i = 0 ; i < weapons.Length; i ++) 
          {
                 //Set All weapons inactive
                  weapons[i].SetActive(false);
          }

          //Test to ensure that the desired index does not exceed the array length
         if(weaponID < weapons.Length)
         {
              //Set the desired weapon to be active
              weapons[weaponID].SetActive(true);
         }
 
      }



Hope that helps! (And apologies for the wonky code-formatting)

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 NerdRageStudios · Oct 13, 2015 at 09:06 AM 0
Share

Fantastic, that worked a charm and is certainly a lot simpler too :)

avatar image
3

Answer by _Adriaan · Oct 12, 2015 at 11:32 PM

Why not:

      public void ChangeWeapon(int weaponID)
      {
          for (int i = 0 ; i < weapons.Length; i ++) 
              weapons[i].SetActive(i == weaponID);
      }

?

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 CharlieMcF · Oct 13, 2015 at 06:50 AM 0
Share

Yea, that's much neater :P

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

35 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

Related Questions

Comparing each random element in an array with each other element 3 Answers

Array index confusion - what does [i-1] do exactly??? 4 Answers

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

Adding a texture to array textures?! 1 Answer

How to save a 2d-array in C# 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