- Home /
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; }
Just a note: You can use Random.Range ins$$anonymous$$d of math.floor and all that.
Oh! Yes. I don't know why I wasn't wasn't using that function. Thanks for your help. Doing that seems to have changed the error, magically. I guess my computer is named Dave, then, ironically...2001, anyone?
Answer by Eric5h5 · May 29, 2011 at 07:35 PM
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.
So, this is a lot of good stuff to know. I'll try to incorporate all this into this project, or at least the next one. Changing the Random.value to Random.Range has changed the error to a "NullReferenceException: Object reference not set to an instance of an object" error, so that's interesting.
I guess a lot of the stuff I do is just things that I started doing because I found it worked, and I was desperate for anything--I just started coding JS a few months ago, and haven't done any real studying of it.
Does keeping variables local significantly reduce the memory usage?
Cheers
Simon
I guess array_being_used isn't defined...at the start of the SetNewPlacard function, do
var array_being_used : Transform[];
I doubt local variables has much effect on memory usage; you would use them for the other reasons I mentioned. Also they can be slightly faster if local.
Your answer
Follow this Question
Related Questions
Typecast a Generic List in UnityScript 1 Answer
problem with selecting characters script 0 Answers
Iterating a GO through an array of transform positions 0 Answers
Dynamic array length problem.Why my code generate error? 1 Answer
.Shift() error 0 Answers