Search Unity

What is #pragma implicit and #pragma downcast

Discussion in 'Scripting' started by Jon-Huhn, Oct 8, 2010.

  1. Jon-Huhn

    Jon-Huhn

    Joined:
    Jan 26, 2009
    Posts:
    85
    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!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    #pragma implicit allows you to implicitly declare variables when using #pragma strict.

    Code (csharp):
    1. #pragma strict
    2. foo = 5; // no!
    3.  
    4. #pragma strict
    5. var foo = 5; // yes!
    6.  
    7. #pragma strict
    8. #pragma implicit
    9. foo = 5; // yes!
    #pragma downcast allows casting from a super type to a sub type when using #pragma strict.

    Code (csharp):
    1. #pragma strict
    2. var go : GameObject;
    3. var clone : GameObject = Instantiate(go); // no!  Instantiate returns Object, not GameObject
    4.  
    5. #pragma strict
    6. #pragma downcast
    7. var go : GameObject;
    8. var clone : GameObject = Instantiate(go); // yes!
    9.  
    10. #pragma strict
    11. var go : GameObject;
    12. 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
     
  3. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    ?

    Doesn't #pragma implicit nullify #pragma strict ?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Eh? No....

    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.     var foo = GetComponent(Transform);
    5.     foo.position.x = 100.0;  // no!  Can't use dynamic typing with #pragma strict!
    6. }
    #pragma implicit wouldn't have any effect there. (Might want to use #pragma downcast though, depending on how you fix the dynamic typing.)

    --Eric
     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    What's wrong with this line ? We just assign 100.0 to the x variable of a transform.position :-|
     
  6. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    "foo" has no type, i.e. is not explicitly a Transform.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  8. Toxic Blob

    Toxic Blob

    Joined:
    Jun 8, 2010
    Posts:
    18
    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.

    Code (csharp):
    1. var mesh : Mesh = new Mesh ();
    2. 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?
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. (GetComponent(MeshFilter) as MeshFilter).mesh = mesh;
    Or use generics:

    Code (csharp):
    1. GetComponent.<MeshFilter>().mesh = mesh;
    --Eric
     
    Last edited: Oct 14, 2010
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    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?
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Those are meant to be separate examples.

    --Eric
     
  12. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Got it. thought I might have been missing something there for a sec :)
     
  13. temo_koki

    temo_koki

    Joined:
    Jul 14, 2009
    Posts:
    308
    how would be this for enabling script, for example I want to disable/enable script "chack" attached to game object. I have such line:
    PHP:
    child.gameObject.GetComponent("chack").enabled=true;
    and it gives me such error: 'enabled' is not a member of 'UnityEngine.Component'. when I write:
    PHP:
    child.gameObject.GetComponent.<"chack">().enabled=true;
    it gives me this error: Unexpected token: chack.
     
    Last edited: Nov 3, 2010
  14. temo_koki

    temo_koki

    Joined:
    Jul 14, 2009
    Posts:
    308
    heh, it doesn't need quotes :D
    child.gameObject.GetComponent.<chack>().enabled=true;
     
    Last edited: Nov 3, 2010