Why does it always say 'Index was outside the bounds of the array.' when i put a gameobject in an array

image

Arrays can’t grow in size and need to be initialized beforehand.

You’re most likely never initializing the array, hence it has a length of null (not to be confused with 0) which means that you’ll always be outside of the bounds of the array when putting something in the 0th spot.

1 Like

yeah. you are right.I made it public and gave it size,and then it can be used normally.But it’s amazing that I also defined a GameObject array before, and I didn’t give it a size, and it can add elements later. But when I put the new array function behind it, the old array also reported an error.After I reverse the version, the old array can be used again :joy: