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 /
This question was closed Aug 29, 2013 at 02:17 PM by -hiTo- for the following reason:

The problem went away after reopening Unity...

avatar image
0
Question by -hiTo- · Aug 29, 2013 at 01:35 PM · arrayinspectorout of range

How to check if inspector-filled array is empty

Hello! This is probably the easiest question ever, but I'm stuck, so any help would be nice.

I have an array that I fill out in the inspector with values. It's an array of my own Serialized class. In that class, I have another array of the same class. Like so:

 [System.Serializable]
 public class RoundInfo
 {
     
     public RoundInfo[] MetaRound;

Now I want to combine these multiple arrays into 1. There will always be at most 2 arrays to combine, so it shouldn't be a problem. Here's the code for it:

 if (Rounds[currentLevel].MetaRound.Length > 0) // Convert 1 Round Info and 1 array of Round Infos into 1 array
         {
             currentRoundInfo = new RoundInfo[Rounds[currentLevel].MetaRound.Length + 1];
         }
         else
         {
             currentRoundInfo = new RoundInfo[1];
         }
         
         currentRoundInfo[0] = Rounds[currentLevel];
         for (int i = 1; i < currentRoundInfo.Length; i++)
         {
             currentRoundInfo[i] = Rounds[currentLevel].MetaRound[i];
         }

However, it gives me an "Array index out of range" when trying to access MetaRound on line #1, when MetaRound is empty in the inspector. I need MetaRound to be able to be empty.

I'm guessing it's because it never gets initialized. But how can I check if it's empty, if Length > 0 doesn't work? If I set it through code, this works like a charm, but since I need the inspector (1000 times smoother to work with)... well.. it's a bitch.

I've come up with 1 way to work around it, and that's to use a List instead - but is it really necessary?

Comment
Add comment · Show 12
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 Polymo · Aug 29, 2013 at 01:44 PM 0
Share

just a wild guess: you may check if currentLevel < Rounds.Length before accessing something in it.

avatar image moghes · Aug 29, 2013 at 01:46 PM 0
Share

Are you sure it is telling you out of range for $$anonymous$$etaRound? what about Rounds[currentLevel] ? where are you declaring this array? can you copy and past the error from the console please

avatar image -hiTo- · Aug 29, 2013 at 01:49 PM 0
Share

This should answer both your questions: putting a Debug.Log(Rounds[currentLevel]); right above the if statement prints out just fine.

At this time, I don't have a function that increments currentLevel at all, so it's stuck at 0, until I get this to work as intended.

IndexOutOfRangeException: Array index is out of range. Level.NewLevel () (at Assets/Other/Scripts/Logic/Level.cs:99)

avatar image -hiTo- · Aug 29, 2013 at 01:55 PM 0
Share

Hmm.. Something is seriously weird though...

I just tried printing Rounds[currentLevel].$$anonymous$$etaRound.Length

And it printed out 0. Without any error...

avatar image moghes · Aug 29, 2013 at 01:56 PM 0
Share

well then can we see the portion of code in Level.cs its telling line 99, but past the whole NewLevel() function please

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Arrays and the inspector 1 Answer

Array not updating in inspector 0 Answers

How to make a custom type's contents modifiable in an array by inspector? 1 Answer

Array index is out of range Error, only when array is used to Instantiate 4 Answers

How do I make a public array of arrays appear in the inspector? 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