What is #pragma implicit and #pragma downcast

In the process of automatically converting my 2.6 project into 3.0, Unity has added

#pragma implicit
#pragma downcast

to all my scripts. Does anyone know why and what these do? The shipped help docs don’t say anything about these.

Thanks!

#pragma implicit allows you to implicitly declare variables when using #pragma strict.

#pragma strict
foo = 5; // no!

#pragma strict
var foo = 5; // yes!

#pragma strict
#pragma implicit
foo = 5; // yes!

#pragma downcast allows casting from a super type to a sub type when using #pragma strict.

#pragma strict
var go : GameObject;
var clone : GameObject = Instantiate(go); // no!  Instantiate returns Object, not GameObject

#pragma strict
#pragma downcast
var go : GameObject;
var clone : GameObject = Instantiate(go); // yes!

#pragma strict
var go : GameObject;
var clone : GameObject = Instantiate(go) as GameObject; // yes!

The #pragma strict in Unity 2.6 wasn’t as strict, so it was essentially using downcast and implicit.

–Eric

?

Doesn’t #pragma implicit nullify #pragma strict ?

Eh? No…

#pragma strict

function Start () {
	var foo = GetComponent(Transform);
	foo.position.x = 100.0;  // no!  Can't use dynamic typing with #pragma strict!
}

#pragma implicit wouldn’t have any effect there. (Might want to use #pragma downcast though, depending on how you fix the dynamic typing.)

–Eric

What’s wrong with this line ? We just assign 100.0 to the x variable of a transform.position :expressionless:

“foo” has no type, i.e. is not explicitly a Transform.

Try the code and see what the error is.

Actually no, GetComponent returns Component, so “foo” is of type Component, which has no position variable.

–Eric

So what is the best procedure then? To use #pragma downcast, or to adjust the line to not return a Component but a Transform? Code like the following (from the documentation) no longer work without having #pragma downcast.

var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;

So, without using a #pragma downcast, how would I tweak that so I don’t get the error about Component not having a .mesh attribute?

(GetComponent(MeshFilter) as MeshFilter).mesh = mesh;

Or use generics:

GetComponent.<MeshFilter>().mesh = mesh;

–Eric

Hey guys, just wondering, in your sections of example code, you have #pragma strict on multiple lines. I thought it only had to be stated once at the top of each script?

Those are meant to be separate examples.

–Eric

Got it. thought I might have been missing something there for a sec :slight_smile:

how would be this for enabling script, for example I want to disable/enable script “chack” attached to game object. I have such line: child.gameObject.GetComponent("chack").enabled=true;and it gives me such error: ‘enabled’ is not a member of ‘UnityEngine.Component’. when I write: child.gameObject.GetComponent.<"chack">().enabled=true; it gives me this error: Unexpected token: chack.

heh, it doesn’t need quotes :smile:
child.gameObject.GetComponent.().enabled=true;