Type casting error while instantiating.

Hey there!

So, I’m getting an error with this script in which I’m attempting to call a random object from an array. The code is throwing an error, saying that

 InvalidCastException: Cannot cast from source type to destination type.
Boo.Lang.Runtime.RuntimeServices.GetArraySlice (System.Object target, System.Object[] args)

and pointing to the line in which I actually instantiate a new object and store it in the newplacard variable. The transforms are all assigned in the inspector, and I’m just not sure why it’s not working at this point. The program can print what the array_being_used variable is, what the randomplacard variable is, but it can’t look at what the two are together…

Any ideas, folks? I will name my computer after you if you can help me out.

Cheers,

Simon

Code:

var randomx;
var ycoord = .7;
var randomz;
var howmanyplacards = 20;
var randomplacard;
var newplacard;
var DifficultyLevel = 0;

var eyeofbeholder: GameObject;
var destroyer;
destroyer = eyeofbeholder.GetComponent("Character Destroyer");

var a: Transform;
var b: Transform;
var c: Transform;
var d: Transform;
var e: Transform;
var f: Transform;
var g: Transform;
var h: Transform;
var i: Transform;
var j: Transform;
var k: Transform;
var l: Transform;
var m: Transform;
var n: Transform;
var o: Transform;
var p: Transform;
var q: Transform;
var r: Transform;
var s: Transform;
var t: Transform;
var u: Transform;
var v: Transform;
var w: Transform;
var x: Transform;
var y: Transform;
var z: Transform;

var apple: Transform;
var kiwi: Transform;
var fig: Transform;
var pear: Transform;

var banana: Transform;
var cherry: Transform;
var grapes: Transform;
var lychee: Transform;

var strawberry: Transform;
var watermelon: Transform;
var pomegranate: Transform;
var pineapple: Transform;

var alphabet: Array;
var easy_fruits: Array;
var medium_fruits: Array;
var difficult_fruits: Array;
var alphabet_and_fruits: Array;
var array_being_used: Array;

function Awake()
{
	alphabet = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z];
	easy_fruits = [apple,kiwi,fig, pear];
	medium_fruits = [banana, cherry, grapes, lychee];
	difficult_fruits = [strawberry, watermelon, pomegranate,pineapple];
	alphabet_and_fruits = [apple,kiwi,fig, pear, banana, cherry, grapes, lychee, strawberry, watermelon, pomegranate,pineapple, a, b, c, d, e, f, g, h, i, c, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z];
}

function SetNewPlacard() 
{
	if(DifficultyLevel == 0) array_being_used = alphabet;
	if(DifficultyLevel == 1) array_being_used = easy_fruits;
	if(DifficultyLevel == 2) array_being_used = medium_fruits;
	if(DifficultyLevel == 3) array_being_used = difficult_fruits;
	if(DifficultyLevel == 4) array_being_used = alphabet_and_fruits;
	
	randomplacard = Mathf.Floor(((Random.value) * array_being_used.length)-1);
	print(randomplacard);
	randomx = (Random.value * 30) -15;
	randomz = (Random.value * 300)-700;
	
	newplacard = Instantiate (array_being_used[randomplacard], Vector3(randomx, ycoord, eyeofbeholder.transform.position.z+40),  Quaternion.identity);
	newplacard.transform.Rotate(0, 180, 0);
	newplacard.tag = "Ground Placards";
}

var Level_From_GameData;

function SetDifficultyLevel(Level_From_GameData:int)
{
	DifficultyLevel = Level_From_GameData;
}

Instead of having 10 million separate variables like that, arrays should be declared like this:

var alphabet : Transform[];
var easy_fruits : Transform[];
var medium_fruits : Transform[];
var difficult_fruits : Transform[];
var alphabet_and_fruits : Transform[];

Then you can get rid of most of those variables, as well as the Awake function. Using “Array” for arrays is untyped and slow, so you wouldn’t use it unless you were mixing objects of different types in the same array. Use built-in arrays like Transform instead (or List if the array needs to be resizable). Also, stay away from things like this:

var randomx;

Instead, specify a type, or assign a value so the type can be inferred.

var randomx : int;
var randomx = 0;

Also, this:

randomplacard = Mathf.Floor(((Random.value) * array_being_used.length)-1);

would be easier like this:

var randomplacard = Random.Range(0, array_being_used.length);

Finally, all variables should be local to functions if possible. Don’t declare variables outside functions unless they are global variables that are used in multiple functions. Keeping variables local makes the code more readable and less likely to have bugs.