The following script works fine before I add #pragma strict and produces the following error when it is added.
Cannot convert ‘UnityEngine.Object’ to ‘UnityEngine.GameObject’.
I’m pretty sure that this is a syntax issue but I haven’t been able to find any examples to draw from. Assistance much appreciated.
#pragma strict
var BigRock : GameObject;
function Update () {
//Create Big Rock
var RockClone : GameObject;
RockClone = Instantiate(BigRock, transform.position, transform.rotation);
}
Instantiate returns an object of class UnityEngine.Object, if you want to make it a game object, add “as GameObject” before the ;
Works like a charm. Thanks. I seem to be running into these syntax problems more regularly as i attempt to develop for iOS so hopefully I’ve figured out what the problem is. Can someone confirm (or correct) the following.
With #pragma strict I must address each element in a Class whereas without #pragma strict it is possible to sometimes leave certain things empty.
Example.
My error above is because I didn’t address the final : Object.
static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
Is there a reference that illustrates the difference between using and not using #pragma strict?
#pragma strict will disable dynamic typing and automatic type casting basically. If something is Class A and you want it to get to Class B and the situation is in the way that B extends from A then you must manually cast it, because its not granted that A is of class B (the compiler can not guarantee it), so its up to you to cast and verify that it is.
If the situation where that A extends B then you can assign it cause casting to a base class will always work, as A is also of class B aside of being of class A
Generally you can look up at C#, with pragma strict / iOS, the casting basically works like it does on C#
The type declaration for : GameObject is technically not required. An untyped variable will become an instance of the class that the first thing you assign to is has.
So if you have
var blub;
blub = new GameObject();
then blub on the first line does not know what it is, but from the moment on we assign the new game object to it, blub will be a GameObject and a GameObject only, it will not change its type again.
Naturally declaring it is not wrong nor bad, it ensures that you know right from the start what will be in there, you can otherwise end with funny phantom issues if you let the first assignement declare it
Thanks for the clarification. I’ll have to stare at it a bit before it sets in 