What this code does is basically picking a random game object from a array and summoning a function to move it, and it works fine.
Bug 1: But it if I do the exact same code without the “+ 0”, it returns the error “array out of range”, which for me makes no sense since the value is the same.
Bug 2: If I type “gameobject.” in Monodevelop, “Length” is not an option for picking, and yet it works. It probably may confuse other new programmers like me.
Hope it helps to improve even more this amazing program and community
You should not use “gameObject” as a variable name, since that’s already used by Unity to refer to the GameObject that the script is attached to. Also it’s a good idea to name variables appropriately for the variable type, so you’d use some kind of plural name for arrays (since there’s more than one entry in most arrays).
If you have 1 object in the array, it will be at position 0, but the length will return 1. There is a discrepancy of 1 that you need to account for when using an arrays length as an index.
You should also check to see if an array has anything in it before using it.
It should not be; Random.Range is non-inclusive when used with integers. Random.Range(0, myArray.Length) is correct. (Which maybe is the reason for the original error: if you actually used + 0.0f rather than + 0, that would make Random.Range use the float version, which is inclusive.)
The Random function can’t return the value you set as max, the true max will be one below it, so if I use -1 it will never get the latest position of the array.
And yes, I have a check for empty arrays
You didn’t understand the problem… I need to use + 0 for the code to succeed. It returns an array index error if I do this instead of what I wrote before: