Object > gameobject

Hi,

I’m having a huge headache which I can’t seem to find the right answer for (in JS).

var a : GameObject; // or been made somewhere else...
var b : GameObject;

b = Instantiate(a, transform.position, Quaternion.identity);

Then I get an error in the console:

Cannot convert UnityEngine.Object to UnityEngine.GameObject

so I can only guess that the ‘object’ which Instantiate accepts makes the returning value an object, but how to do change it as I need the returning value to be a GameObject.

I tested this by changing [ var b : Object ] and it works, however breaks later on in the code.

Hope that makes sense.

Thanks

Dan.

Try Transform instead of GameObject as the var type.

Hi,

I just did, it complained about it but with the Transform instead.

I put . after the Instantiate bracket and its not complaining… I’ll continue then build and see if it works :slight_smile:

Thanks

Dan.

Well now I get node unityengine.object.instantiate(self.a, self.get_transform().get_position(), Quaternion.get_identity())[of UnityEngine.GameObject] has not been correctly processed.

back to the drawing board :frowning:

b = Instantiate(a, transform.position, Quaternion.identity) as GameObject;

http://answers.unity3d.com/questions/35496/cannot-implicitly-convert-type-unityengine-object-to-unityengine-gameobject

ahhhhhhh … I saw that, but I thought it was part of some c# code so ignored it.

Thanks :smile:

Dan.

It’s always necessary in C#, but only necessary in JavaScript when you need strict typing, which of course you do, in this forum section, for the time being. It’s a good practice to get into, in my opinion. Someday we’ll surely have dynamic typing available for iOS, but I’ll keep using strict typing. It doesn’t take much extra time to type, and it makes the code more clear to me. Countless others agree and disagree! :slight_smile:

Well I don’t mind as long as it works!! :slight_smile:

It would be nice to have some breakpoints though so you can run through the code :slight_smile: