Array length bug (C#)

Hello!

I’m new to both C# and Unity, so it may not be really a bug, but I’m almost 100% sure it is.

private GameObject[] gameobject;	
gameobject = GameObject.FindGameObjectsWithTag("tag");
SetTarget(gameobject[Random.Range(0, gameobject.Length + 0)].transform.position);

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 :slight_smile:

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).

–Eric

These are just sample names, my actual variables are named differently and the code works fine :slight_smile:

Maybe some kind of rounding error.

But it should actually be gameobject.Length - 1

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.)

–Eric

Intradesting

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 :slight_smile:

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:

SetTarget(gameobject[Random.Range(0, gameobject.Length)].transform.position);

(note that I just removed the “+ 0”)

EDIT: To be a little more specific…
This works:

private GameObject[] gameobject;   
gameobject = GameObject.FindGameObjectsWithTag("tag");
SetTarget(gameobject[Random.Range(0, gameobject.Length + 0)].transform.position);

But it does not:

private GameObject[] gameobject;   
gameobject = GameObject.FindGameObjectsWithTag("tag");
SetTarget(gameobject[Random.Range(0, gameobject.Length)].transform.position);

I could not reproduce this error.

private GameObject[] gameobject;
....
gameobject = GameObject.FindGameObjectsWithTag("Tree");
Debug.Log("GameObject" + gameobject[Random.Range(0, gameobject.Length)].transform.position);

Also works fine with +0
+0.0f complains about implicit casting with the Length

I’m using v4.2.0

Mine too. Weird, there must be a code issue here then…

It’s just getting the types required by Random.Range() to match up - it will take either two floats or two ints