Not a question, but figured I’d give everyone a heads up.
Yes I realize that there is no point to an array with a length of 1, but this may come up as an issue (as in my case) if you’re creating a script that uses a variable to initialize the length of an array. Just make sure that you use
if(myvar < 3) myvar =1;
myArray = new arraytype[myvar];
It doesn’t really crash the program, so much as it just doesn’t allow anything in that function after the array initialization to run, which can create some pretty weird and seemingly irrelevant issues.
Happy coding!
I don’t quite get what you say here. An array with length 1 does work just as fine as any other array. Even an array with length 0 is allowed and also useful in certain cases. A good example is GetComponents. It returns an array of the components it has found. If there’s only one component that matches the required type, the returned array will have a length of 1 and only contains that one component.
However if there is no such component at all, the method will return an array of length 0. You can use any common method to iterate through that array and everything should work just fine. If the length is 0 a foreach loop or a normal for loop would not do a single iteration since the array is empty.
So your “advice” (if there is any) is at best confusing and in the worst case completely misleading.
ps: The condition you used is a bit confusing if your variable is smaller than 3 you set it to 1. The 3 and 1 are so random arbitrary choices here so without context this just looks weird.