- Home /
Object to int
I use an Array to load int values and shuffle. But when I put "#pragma strict" and get a value of the array I get the error: BCE0022: Cannot convert 'Object' to 'int'.
#pragma strict
var numbers : Array;
numbers = new Array(6);
var x : int = Random.Range(1,10);
numbers[0] = x;
...
var n : int;
n = numbers[0];
Maybe I could use an int vector "int[]". But I have made the entire game without "#pragma strict" and also I use the method Array.splice.
Answer by Eric5h5 · Jul 05, 2011 at 08:07 PM
Don't use Array, which slow and not type-safe. Use List instead, then you don't have to worry about casting.
#pragma strict
import System.Collections.Generic;
var numbers = new List.<int>();
for (var i = 0; i < 6; i++) {
numbers.Add(Random.Range(1, 10));
}
...
var n = numbers[0];
A question for @Eric5h5: Why do you say Array is slow as compared to List? Since Array is fixed in size it would seem simpler and therefore faster. Is it because it is an array of object, and therefore must cast each access? The OP suggested using int[] which would avoid the cast.
note that Array is a class. not to be confused with an 'array' (lowercase).
http://unity3d.com/support/documentation/ScriptReference/Array.html
@hellcats: Array isn't fixed size, it's basically ArrayList. int[] is a fixed-size int array (and faster than List), but the OP mentioned splice, which you can't do with a fixed-size array. And yes, boxing and unboxing is slow.
Your answer
Follow this Question
Related Questions
How to pass a int from a object to another 1 Answer
pragma strict issues with iOS 1 Answer
'gameObject' is not a member of 'Object'. 1 Answer
Cast object to monobehaviour 1 Answer
C# Int to Float conversion performance 3 Answers