- Home /
The problem went away after reopening Unity...
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?
just a wild guess: you may check if currentLevel < Rounds.Length before accessing something in it.
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
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)
Hmm.. Something is seriously weird though...
I just tried printing Rounds[currentLevel].$$anonymous$$etaRound.Length
And it printed out 0. Without any error...
well then can we see the portion of code in Level.cs its telling line 99, but past the whole NewLevel() function please
Follow this Question
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