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 /
  • Help Room /
avatar image
0
Question by Naelya · Apr 19, 2016 at 11:22 PM · arrayarray-out-of-range-except

Array index is out of range C#

|EDIT| I changed the name of days[] to dayOfWeek[] and that seems to have fixed it? Does Unity use "days" for anything else? I couldn't find it anywhere in the API list.. Thanks for the help anyway!

//

Feels like I've read through almost a hundred threads with the same question, but I'm still stuck with my error, so here goes:

I have this really simple script with 2 arrays to basically run through 16 days. For now I just wanted to log them to see if they work as intended, but I'm getting the index out of range error.

This is my code:

     private string[] timeOfDay;
     int timeOfDayCounter = 0;
 
     private string[] days;
     int dayCounter = 0;
     
     void Start () {
         timeOfDay = new string[4] { "Morning", "Afternoon", "Evening", "Night" };
         days = new string[16] { "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Day 8", "Day 9", "Day 10", "Day 11", "Day 12", "Day 13", "Day 14", "Day 15", "Final Day"};
     }

     public void DisplayTime()
     {
         Debug.Log(days[dayCounter]);
         Debug.Log(timeOfDay[timeOfDayCounter]);
     }
     
     public void UpdateTime()
     {
         timeOfDayCounter += 1;
         if (timeOfDayCounter > 3)
         {
             timeOfDayCounter = 0;
             dayCounter += 1;
         }
     }
 

DisplayTime and UpdateTime are called in a separate script on click (in that order), and the first time it runs the first line in DisplayTime, it throws the error.

I've tried setting the arrays as private, but it didn't help. The arrays exist in the inspector when I start the game, so they're definitely not empty. alt text

If I log the Counters, they seem to work as intended. I've tried using a couple set numbers to log the arrays, but it still gives the error. A friend told me to try and log them as this.array[#], but that didn't do it either.

I feel like I'm missing something incredibly obvious, I'm just trying to see what's in my arrays?

arrays.png (19.6 kB)
Comment
Add comment · Show 1
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 jgodfrey · Apr 20, 2016 at 12:22 AM 0
Share

If this line:

 Debug.Log(days[dayCounter]);

... is throwing the mentioned error, then "dayCounter" must be outside of the range of 0-15. Write out the value of "dayCounter" as the first thing in DisplayTime to verify that.

The above code doesn't seem to be managing the valid range of the dayCounter - how do you ensure that it's in the 0-15 range?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Immanuel-Scholz · Apr 20, 2016 at 04:34 PM

Two problems here:

  1. Your timeOfDayCounter - overflow check is "> 4", but any C# array starts with 0, so if timeOfDayCounter == 4, it is already too late. You want to check to ">= 4".

  2. You never check dayCounter. You probably just missed a similar check that resets the dayCounter to 0 if its >= 16.

Btw.. another way of increasing a counter that should keep inside an arrays bound is:

 timeOfDayCounter = (timeOfDayCounter + 1) % timeOfDay.Length;

The % - operator will ensure that the value goes back to 0 if it reaches the array length. (Note, that this trick does not work with decrementing. The % operator will go negative)

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 Naelya · Apr 20, 2016 at 05:51 PM 0
Share

Sorry, I should've probably clarified that it errored on the first check (when both counters were still 0). I noticed after posting that timeOfDayCounter would've gone one check too far indeed, but it never reached that point anyway. dayCounter doesn't have a check yet, and I understand that it should definitely error if it got past that point, but it never got past 0.

I didn't know about that % - operator though, that's very useful, thanks! Although for this code in particular, that means my if-statement won't run anymore, so it won't increase the dayCount anymore (or I'll have to do it early).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array index out of range 1 Answer

Accessing the array of a null item / Moving items in array 0 Answers

Fill Array Of Arrays With Method/Function 0 Answers

Array index is out of range? 4 Answers

IndexOutOfRangeException: Index was outside the bounds of the array. (wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int) 0 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