Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Spider_newgent · Aug 21, 2013 at 08:16 AM · arrayexceptionmousewheel

"Array out of range" when cycling weapons.

I'm using the mousewheel to cycle through an array of 3 (so far) weapons. on mousewheel up it cycles up through the list, and on mousewheel down it counts down.

This works fine, and prints the weapon currently selected to the terminal.

However, if weapon 3 is selected and the mousewheel pushed up, it throws an exception, "array out of range." Now obviously that's because the variable has increased to 4 and there are only 3 objects in the array, but I can't figure a way to stop it.

Putting the following in my update phase doesn't work:

 if (weaponNumber < 0)
         {
         weaponNumber = weapons.Length;
         }
         
         if (weaponNumber > weapons.Length)
         {
         weaponNumber = 0;
         }

I've searched around but can't find a solution. I'm sure it's something really simple I'm missing.

Thanks

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

Answer by Joyrider · Aug 21, 2013 at 08:18 AM

you should change your second if to

 if (weaponNumber >= weapons.Length)
 {
     weaponNumber = 0;
 }

because if you have 3 slots, in an array, numbering start at 0, so 0,1,2. And in your current code, you can still get 3. (which throws the error)

Comment
Add comment · Show 6 · 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 Spider_newgent · Aug 21, 2013 at 08:27 AM 0
Share

Ha ha, that was even simpler than I had thought!

Ok, I'm half way there. Using $$anonymous$$ouse Wheel Up now correctly cycles through the array changing from the highest back to 0 and so on.

However, cycling down gets stuck at zero. The code:

 if (weaponNumber < 0)
        {
        weaponNumber = weapons.Length;
        }

doesn't change the index to the last/highest object in the array, even if I use "weaponNumber <= 0" ins$$anonymous$$d of just "weaponNumber < 0".

On a plus point, it doesn't throw an exception any longer :)

avatar image Spider_newgent · Aug 21, 2013 at 08:34 AM 0
Share

I've played around a bit more and if I manually set it up like this:

 if (weaponNumber < 0)
        {
        weaponNumber = 2;
        }

Then it works. Therefore it seems my problem is with setting:

 weaponNumber = weapons.Length;

As this is a different problem, I'll post it as a new question.

avatar image Joyrider · Aug 21, 2013 at 08:35 AM 0
Share

here you should use

 if (weaponNumber < 0)
 {
    weaponNumber = weapons.Length-1;
 }

for the same reason... max id is 2 and your length is 3 (0,1,2)

avatar image fafase · Aug 21, 2013 at 08:38 AM 0
Share

your last solution works but is not really good. If you add a weapon you need to modify the script.

    if (weaponNumber < 0)
    {
       weaponNumber = weapons.Length - 1;
    }

should fix your problem. Remember that an array has length 3 but the last index is 2. So 3 is out of bounds

avatar image Spider_newgent · Aug 21, 2013 at 08:41 AM 0
Share

Understood.

Looks like I need to refresh myself on arrays.

It works perfectly now.

Thankyou for your speedy help.

Show more comments

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

16 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

Related Questions

int array overflow exception 1 Answer

Uncaught exception doesn't kill the application 5 Answers

A null value was found where an object instance was required. 1 Answer

Null Reference Exception After refering to an Array 1 Answer

Instantiating random prefabs 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