• 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 justinurie · Oct 14, 2022 at 07:45 PM · arraysinventoryfor-loopindexindexing-array

Most efficient way to index through an array, skipping nulls

Hi folks
I am trying to implement a hotbar for my player inventory (game is played on controller) thus they use RB/LB to navigate the hotbar, but I want the $$anonymous$$ghlighter to skip to the next spot with an item, ignoring empty spots. I am pretty sure Minecraft works t$$anonymous$$s way, so you have an idea of what I'm trying to do.

Anyway, I have written the following block of code that is called in my Input function that is called in Update, so t$$anonymous$$s is happening every frame. I am afraid that t$$anonymous$$s is an inefficient way to handle t$$anonymous$$s, but I can't t$$anonymous$$nk of a better way to do t$$anonymous$$s.

My code below appears to work fine but I fear checking t$$anonymous$$s every frame is bad If anyone has any insight or ideas it would be much appreciated!

         if (rightBumperBool)
         {
             for (int i = inventoryIndexer; i < gunInventory.Length-1; i++)
             {
                 if (gunInventory[i + 1] != null)
                 {
                     inventoryIndexer = i + 1;
                     break;
                 }
             }
         }
         if (leftBumperBool)
         {
             for (int i = inventoryIndexer; i > 0; i --)
             {
                 if (gunInventory[i - 1] != null)
                 {
                     inventoryIndexer = i - 1;
                     break;
                 }
             }    
 
         }

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
0
Best Answer

Answer by IvanC314 · Oct 14, 2022 at 08:17 PM

Your code seems pretty fine as long as you turn your bools rightBumper and leftBumper back to false after they are turned true as to stop the for loops from running more than once. I don't t$$anonymous$$nk there's an issue with your code but you could try using getinput

 Update()
 {
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         for (int i = inventoryIndexer; i < gunInventory.Length - 1; i++)
         {
             if (gunInventory[i + 1] != null)
             {
                 inventoryIndexer = i + 1;
                 break;
             }
         }
     }
     else if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         for (int i = inventoryIndexer; i > 0; i--)
         {
             if (gunInventory[i - 1] != null)
             {
                 inventoryIndexer = i - 1;
                 break;
             }
         }
     }

You can switch the keycodes out to your preferred buttons. Getting input is fine in update. Hope t$$anonymous$$s helps.

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 justinurie · Oct 14, 2022 at 08:28 PM 1
Share

The bools are already doing GetKeyDown essentially and only are True for exactly 1 frame then revert to 0, so I guess I am doing everything right :)

thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

150 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 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

Array index is out of range? 1 Answer

[C#]For-loop in For-loop crashes Unity. 1 Answer

Creating an inventory/weapon array and ability to scroll through it 1 Answer

JavaScript Grid Array Size 0 Answers

Is this considered bad coding 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