InvalidCastException on Instantiate

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!

Hi, Insantiate returns type “object” so compiler getting confused that you are trying to store it in type Transform.

I see you are using javascript so I am not sure of the exact syntax but you must cast the result as a Transform to store it in your variable.

e.g.

gob = Instantiate(p_walls, Vector3(x, 0, y), Quaternion.identity) as Transform;

OR

gob = (Transform)Instantiate(p_walls, Vector3(x, 0, y), Quaternion.identity);

Thanks for the quick reply.

If I do

gob = Instantiate(p_walls, Vector3(x, 0, y), Quaternion.identity) as Transform;

It gives me the exact same error. If I try

gob = (Transform)Instantiate(p_walls, Vector3(x, 0, y), Quaternion.identity);

It won’t compile, giving me this error:

Assets/Generator.js(130,42): UCE0001: ‘;’ expected. Insert a semicolon at the end.

And if I try the only other “casting” that I could think to try, basically instantiating a new object, that doesn’t work either:

gob = Transform(Instantiate(p_possible, Vector3(x, 0, y), Quaternion.identity));

Yes, this is UnityScript. I’m putting off learning C# until I need it, especially since UnityScript is supposed to support everything the engine does on all platforms (unlike Boo, which would have been my first choice as I’m a Python programmer by day).

Is there anything else I could try? For my idea to work I would really like to capture the instantiated objects and parent them under a new one.

According to this: Unity - Scripting API: Object.Instantiate it doesn’t seem that you actually need to typecast at all in javascript.

I have just tested it and I can do it the initial way you posted without any errors :confused:

EDIT: It could be a syntax error. “value” is a keyword in C# and maybe it is also in javascript. Try changing that to something else. If that fails the only other thing I can say to try is create a temporary variable to hold the object instead of using the gob object for each one.

Have you set the p_walls value to anything? looks like p_possible is a Transform type variable and it’s not even set to anything… In which case Instantiate wouldn’t know what it is instantiating. Have you assigned a prefab to the p_walls variable?

Also, out of curiosity, why are you instantiating transforms instead of GameObjects?

You can instantiate about anything, including scripts, prefabs, etc. The Unity official documentation for Instantiate even uses Transform in the example code.

Totally. In the outliner, I added an empty, then added the script. Onto the instance of the script I dragged the two objects I’m trying to instantiate.

Interestingly, they instantiate just fine if I’m not trying to catch the result of Instantiate in a variable.

Because when I tried GameObjects, it gave me an error about not being able to convert from GameObjects to the first parameter of Instantiate.

I understand that we can instantiate anything. I was just wondering if this was actually intended.

kikiwunder, try to assign the result of the Instantiate to the Object variable, set the breakpoint and take a look in a debugger what is being returned to you. Then you will know right away why assigning the result to a Transform type variable does not work.

Hi all! I wanted to make sure I came back to thank everyone who responded.

Cdevl, I did a Debug.Log print of the return and it failed with the same error message. That made me wonder if something wasn’t wonky on a deeper level. I deleted the component instance, added a new one, and reattached the correct prefabs. This time it works!

So, something must have gone wrong on file save or during one of the recompiles after editing that caused it to misbehave. Rebuilding has it working like it did before. I’m not sure what specifically led to the bad component instance, but if I can repeat the issue I’ll log a bug.

Whew! I can finally move on with my prototype. Thanks again!!