How to check for an empty array
I have been working on a game for a while, but unity keeps giving me unnecessary error messages which don't effect the function of my game a tall. I have found the problem.
It occurs on this line of code:
if (Movables[i].Right.movableBlock.Length > 0)
When the array Movables is empty and has nothing in any of it's slots.
The error keeps occurring, yet the code works perfectly, so I just need a way to code around the error. Perhaps somehow check for an empty array instead of checking in slot 0 which has nothing in it (the likely cause). I don't know how to do this, so a simple solution would be great.
The error states 'Argument is out of range'
When working with arrays, safety checks are a good practice.
if ($$anonymous$$ovables != null && $$anonymous$$ovables.Length > 0)
{
if ($$anonymous$$ovables[i] != null && $$anonymous$$ovables[i].Right != null)
{
if ($$anonymous$$ovables[i].Right.movableBlock != null &&$$anonymous$$ovables[i].Right.movableBlock.Length > 0)
{
//do stuff
}
}
}
Answer by tanoshimi · May 23, 2016 at 03:20 PM
It's unclear what value you're supplying for i, but if it's an iterator, you should only be increasing it up the maximum length of Movables -1:
for(int i=0; i< Movables.Length; i++){
if (Movables[i].Right.movableBlock.Length > 0)
... do whatever
}
What would really help is if you could give me a line of code which says:
If there is nothing in the array called 'movableBlock' that doesnt create this error
Your error is caused by the logic in the line immediately above:
if ($$anonymous$$ovables [i].Right.fixedBlockPresent || $$anonymous$$ovables [i].targetPos != Vector3.zero) {
i = $$anonymous$$ovables.Count;
}
You're setting i = $$anonymous$$ovables.Count, and then trying to access $$anonymous$$ovables[i].
Array indexers are zero-based, so if $$anonymous$$ovables.Count has, say, 20 elements, then you can access $$anonymous$$ovables[0] - $$anonymous$$ovables[19], not $$anonymous$$ovables[20].
If what you were trying to do was set i to the last element of $$anonymous$$ovables, that line should have read: if ($$anonymous$$ovables [i].Right.fixedBlockPresent || $$anonymous$$ovables [i].targetPos != Vector3.zero) { i = $$anonymous$$ovables.Count - 1; }
If you're not using a loop, then you really need to add some logic at the point you set i, to ensure that it's not beyond the size of the $$anonymous$$ovables array.
Sorry, the statement is actually deeply contained in a loop that I didn't spot. But the 'i' is not the issue. I am checking the other array 'movableBlock', and not '$$anonymous$$ovables', for it's length.
i = $$anonymous$$ovables.Count;
That line is your problem. On the next line you're accessing the array using that index which is 1 beyond the total number of items in the array. Why are you doing that? If you want to break out of the loop then use break.
Your answer
Follow this Question
Related Questions
how do i check if all booleans in an array are false? C# 2 Answers
Check rotation for a Rigidbody2D 0 Answers
Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers
method to display random question in C# 3 Answers
Array sorting issues - Please Help!!! 4 Answers