Unity 4.1.3, Mac 10.8.4.
I feel like a total noob for having to ask this, especially considering I’ve completed a game before in UnityScript!
I’m having a hell of a time passing things back and forth, waaay more than I did in Unity 3.5. I thought it was the fault of #pragma strict, but it doesn’t seem to be. The current issue is that I have a class which has two public variables:
public var p_walls:Transform;
public var p_possible:Transform;
I attach them in the outliner, then run the program. When it gets to this function:
function populate() {
var x:int;
var y:int;
var pos:Array;
var value:String;
var gob:Transform;
for (key in data.Keys) {
pos = hashToLocation(key.ToString());
x = pos[0];
y = pos[1];
value = data[key];
if (value == TL_ROOM) {
// here's the line 130 it mentions
gob = Instantiate(p_walls, Vector3(x, 0, y), Quaternion.identity);
}
else if (value == TL_EXPLORABLE) {
gob = Instantiate(p_possible, Vector3(x, 0, y), Quaternion.identity);
}
gob.parent = this.transform;
}
}
… it fails out with the following error:
InvalidCastException: Cannot cast from source type to destination type.
Generator.populate () (at Assets/Generator.js:130)
I’ve tried everything I can think of, all number of different combinations of castings. I even tried var gob = Instantiate(blah) as Transform. If I’m trying to catch the result of Instantiate in a variable for use afterwards, it errors out. If I don’t catch the result from Instantiate, and remove the line that sets the parent, everything works and there are no errors.
Nothing I’ve found online through Google, even on StackExchange or UnityAnswers, addresses this issue. I know I’ve instantiated prefabs before in this manner in 3.5. Either it’s been so long and I’ve forgotten / I’m missing something, or I’ve bunged something very simple up.
Please help!