- Home /
how to get a random integer in a list?
#pragma strict
var speed = new List.<int>([4,4,20,20,20,20,20,20,20,20,24,24]);
function Start ()
{
index = speed[Random.Range(0,12)];
}
I get argument out of range
Is it normal to use a . in a list declaration in JavaScript? C# would use:
List<int>
yes : Link
it's also the same with generics
// uJS
GetComponent.< $$anonymous$$yType >();
// C#
GetComponent< $$anonymous$$yType >();
Thanks, I'm not overly familiar with JavaScript and it looked strange to me.
And the new? I thought that was a C#
Eric5h5 is far better suited to answer this. uJS is strange where you don't need to use the new keyword, except in the case of collections. When assigning arrays and lists, you need to use the new keyword.
I cannot think of other situations AT$$anonymous$$, (again Eric will know better.) except for a class variable
var myClass : ClassName = new ClassName();
The only thing wrong with the code in the question is hard-coding the value ins$$anonymous$$d of using list.Count. You can omit new in all cases, or you can use it, since it's the same either way:
var myClassInstance = ClassName();
var myClassInstance = new ClassName(); // same thing
except when doing something like creating a collection:
var intArray = new int[10];
Answer by Eric5h5 · Jul 06, 2014 at 04:06 AM
Public variables get their values from the inspector, not your code. Always use the length of the list or array, not hard-coded values, since you don't know for sure how many elements are in the list.