- Home /
Unique # Generation "Cannot cast from source type to destination type."
I'm trying to generate 3 unique random numbers out of 6, i'm getting a "Cannot cast from source type to destination type."
I'm kinda new to this and muddling my way trough. I know it has something to do with trying to pass a variable type in a container not set for it, but I cant for the life of me figure out how to correct this?
any help appreciated.
#pragma strict
var randomNumbers: ArrayList;
function Start ()
{
var aNumbers = new Array(7);
for (var i = 0; i < 7; i++)
{
aNumbers.Add(i);
}
var randomNumbers = new Array[3];
for (i = 0; i < randomNumbers.Length; i++)
{
var thisNumber = Random.Range(0, aNumbers.Count);
randomNumbers[i] = aNumbers[thisNumber]; **//Cast Error Referenced here**
aNumbers.RemoveAt(thisNumber);
}
}
function Update ()
{
print (randomNumbers[0]); **//Cast Error Referenced here**
print (randomNumbers[1]);
print (randomNumbers[2]);
}
Answer by Bunny83 · Jul 24, 2012 at 02:07 PM
This line:
var randomNumbers = new Array[3];
will create a native array of the Array object type. So the elements in the native array would be Array objects. That doesn't make much sense in your case. You want to store integer values (int).
You should avoid the Array class in general since it's slow and not type-safe. Use a generic List instead:
#pragma strict
import System.Collections.Generic;
var randomNumbers : int[];
function Start ()
{
var aNumbers = new List.<int>();
for (var i = 0; i < 7; i++)
{
aNumbers.Add(i);
}
randomNumbers = new int[3]; // create the native array with 3 elements
for (i = 0; i < randomNumbers.Length; i++)
{
var thisNumber = Random.Range(0, aNumbers.Count);
randomNumbers[i] = aNumbers[thisNumber];
aNumbers.RemoveAt(thisNumber);
}
}
function Update ()
{
print (randomNumbers[0]);
print (randomNumbers[1]);
print (randomNumbers[2]);
}
Thank you, That fixed it.
I really appreciate the help.
Answer by BeHappy · Jul 24, 2012 at 11:25 AM
Array[] and Array() both are different...so you are getting the error "Cannot cast from source type to destination type."
So try to change them accordingly and use Array[] and assign values directly in the inspector window of Unity Editor.
That seems to have corrected the casting error, however now it gives me a new error.
when I try to print the results.
print (randomNumbers[0]);
//NullReferenceException: Object reference not set to an instance of an object
any thoughts?
Ins$$anonymous$$d of print try Debug.log().
Debug.Log(randomNumbers[0]);
print is a shortcut for Debug.Log, no wonder (see: http://answers.unity3d.com/questions/10108/debuglog-or-print-whats-the-difference-and-when-to.html)
Answer by C$E Luffy · Jul 24, 2012 at 12:57 PM
you are using a global "randomNumbers" and a local inside the Start(). you are only initializing the local one not the global one. In update function global "randomNumbers" is used which is not iniatialized. remove the "var" keyword from " var randomNumbers = new Array[3];" in Start(). use only "randomNumbers = new Array[3];"in Start().
Your answer
Follow this Question
Related Questions
Store multiple random integers in an array? 4 Answers
Randomly Choosing two objects from an array and switching their positions 3 Answers
C# Array with random.range help? 2 Answers
how to check for GameObject is null in array with random 1 Answer
How can I Instantiate an Object from a Randomly Selected Index in an Array of Game Objects? 1 Answer