Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

in 1.5.1

Discussion in 'Scripting' started by peckinpah, Oct 2, 2006.

  1. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    So, I have this little snippet of code in a script, which tries to access another script, named SET_personproperties:

    var p_data : SET_personproperties;
    p_data = GetComponent (SET_personproperties);


    Later in my script I have this code:

    if (p_data.person_oensker == other.name) {
    p_data.person_oensker_status = 1;
    }

    Trouble is that Unity throws an error about p_data being an "Unknown identifier" when it gets to the first line in the if-sentence...

    I've just upgraded to 1.5.1, so maybe that's the problem. As far as I can remember it worked fine in 1.5.0

    Any suggestions??
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Can you post the entire script the SET_personproperties script that is being referenced.
     
  3. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    sure. It's pretty long, though....

    This script is called TRIGGER_behaviour:
    Code (csharp):
    1.  
    2. var objectType = 0; //0=objektet er en person, 1=objektet er en ting
    3. var soundObject : Transform;
    4. var dosound = 0;  //lydhandtering (speak, lydeffekter, etc)
    5.  
    6. var guitex_messagee : GameObject; //navnet pa det objekt der skal modtage beskeden
    7. var guitex_func = "ShowTex";  //navnet pa den funktion der skal kaldes til at handtere texture
    8. var guitexture = ""; //navnet pa den gui-texture der skal vises
    9.  
    10.  
    11. var guiTexArrayActive = 0;
    12. var guiTexArray = ["", ""];
    13.  
    14. private var triggerCount = 0;
    15. private var triggerCountWithObject = 0;
    16. var playerHasObjectArray = [0,0,0,0,0];
    17.  
    18.  
    19. //opgave-kontrol
    20. var doopgave = 0;
    21. var opgave_type = 0;
    22. var opgave1_level = "";
    23.  
    24.  
    25. //connect til personens scripts
    26. if (objectType == 0) {
    27.     //connect til script der indeholder personens svarmuligheder
    28.     var textresponse : DO_textresponse;
    29.     textresponse = GetComponent (DO_textresponse);
    30.    
    31.     //connect til script der indeholder personens propeties
    32.     var p_data : SET_personproperties;
    33.     p_data = GetComponent (SET_personproperties);
    34. }
    35.  
    36. //ting der skal udføres under samtalen
    37. function OnTriggerStay (other : Collider) {
    38.    
    39.      if (objectType == 0) {    
    40.            
    41.             //tjek om om spilleren giver os et inventory-objekt som vi gerne vil have
    42.             //MANGLER TJEK FOR OM DET REELT ER ET INVENTORY-OBJEKT VI FÅR
    43.        
    44.             //loop alle de objekter som personen oensker igennem
    45.             var minCheck = 0;
    46.             for (i=0;i<=4;i++) {
    47.                 if (p_data.person_oensker[i] == other.name) {
    48.                     minCheck++;
    49.                 } else {
    50.                 }
    51.             }
    52.        
    53.            
    54.             //hvis vi kolliderer med et objekt som vi gerne vil have
    55.             if (minCheck != 0) {
    56.                //skjul inventory-objektet
    57.                print ("skjuler vores objekt");
    58.                invObj = GameObject.Find(other.collider.name);
    59.                if (invObj.layer == 0) {
    60.                    invObj.layer = 8;
    61.                    invObj.transform.parent = null;
    62.                    invObj.rigidbody.freezeRotation = true;
    63.                    invObj.rigidbody.useGravity = false;
    64.                    invObj.rigidbody.isKinematic = true;
    65.        
    66.        
    67.                //noter at inventory-objektet er modtaget
    68.                for (i=0;i<=4;i++) {
    69.                     if (p_data.person_oensker[i] == other.name) {
    70.                         p_data.person_oensker_status[i] = 1;
    71.                         //print ("oenske nr. " + i.ToString() + " har vaerdi : "+ p_data.person_oensker_status[i].ToString());
    72.          
    73.                         //udfør tekst-respons
    74.                         behaviourControl (3, other.name, 1);
    75.                     }
    76.                }
    77.                
    78.                }
    79.            } else {
    80.               //print ("enten giver spilleren os ikke noget eller også er det noget vi ikke vil have");
    81.            }
    82.     }
    83. }
    84.  
    85.  
    86. //ting der skal udføres i starten af samtalen
    87. function OnTriggerEnter (other : Collider) {
    88.  
    89. if (other.tag == "Player") {
    90.  
    91.     //saet global var med personens navn
    92.     if (transform.parent.name.StartsWith("person_")) {
    93.         Master_GameObject_init.FP_currentPerson = transform.parent.name;
    94.     }
    95.  
    96.  
    97.     //tjek at vi ikke kolliderer med irrelevante objekter + udfor opgaver
    98.     if (other.CompareTag("PermObject") == false) {
    99.      
    100.             //tjek om vi skal udfore en opgave
    101.             if (doopgave == 1){
    102.  
    103.                
    104.                 //tjek om vi er en person eller en ting
    105.                 if (objectType == 0) { 
    106.  
    107.             //tjek om spilleren har medbragt nogle af de objekter vi oensker og saet status i et array
    108.             var minobjtemp=0;
    109.                 var i=0;
    110.                     for (i=0;i<=4;i++) {
    111.                         if (Master_GameObject_init.inventory.Contains(p_data.person_oensker[i])) {
    112.                             playerHasObjectArray[i] = 1;
    113.                             minobjtemp++;
    114.                         } else {
    115.                             playerHasObjectArray[i] = 0;
    116.                         }
    117.                     }
    118.                    
    119.                     if (minobjtemp != 0) {
    120.                         //print ("spilleren har et uspecificeret objekt med som vi gerne vil have");
    121.                         playerHasObject = 1;
    122.                     } else {
    123.                         //print ("spilleren har ikke noget med som vi gerne vil have");
    124.                         playerHasObject = 0;
    125.                     }
    126.                    
    127.                 }
    128.  
    129.                 //her er opgaven ikke defineret
    130.                 if (opgave_type == 0){
    131.                 }
    132.  
    133.                 //her skal der loades en guitexture der leder til et andet level
    134.                  if (opgave_type == 1){
    135.                     guitex_messagee.SendMessage ("ShowTex", guitexture);
    136.                     Master_GameObject_init.FP_levelName = opgave1_level;
    137.                 }
    138.  
    139.                 //her skal der loades en gui texture med variabel tekst (PERSON RESPONS)
    140.                 if (opgave_type == 2){
    141.                     if (guitex_messagee) {
    142.                         behaviourControl (playerHasObject, "null", 1);
    143.                     }
    144.                 }
    145.  
    146.                 //her skal der loades en opgave
    147.                 if (opgave_type == 3){
    148.                     print ("load opgave");
    149.                     guitex_messagee.SendMessage ("ShowTex", "opgave1");
    150.                 }
    151.                
    152.                 //her skal der loades en gui texture med variabel tekst (TIL BUDBRINGEREN)
    153.                 if (opgave_type == 4){
    154.                     if (guitex_messagee) {
    155.                         behaviourControl (playerHasObject, "null", 2);
    156.                     }
    157.                 }
    158.        
    159.             }
    160.  
    161.             //tjek om der skal afspilles en lydfil
    162.             if (dosound==1){
    163.                 soundObject.audio.Play();
    164.             }
    165.  
    166.    
    167.     }
    168. }    
    169.  
    170. }
    171.  
    172. //hvis spilleren gar vaek fra objektet afbrydes samtalen
    173. function OnTriggerExit (other : Collider) {
    174.  
    175.  
    176.     //saet global var med personens navn til at vaere blank
    177.     Master_GameObject_init.FP_currentPerson = null;  
    178.  
    179.     if (dosound==1){
    180.         soundObject.audio.Stop();
    181.     }
    182.  
    183.     guitex_messagee.SendMessage ("knap_doCancel", "doCancel"); //fjern evt. grafik ved at kalde generel cancel-funktion
    184. }
    185.  
    186. //denne funktion styrer hvilken respons objektet skal give i form af textures, tekst, etc.
    187. function behaviourControl (playerHasObject, objectName, doType) {
    188.  
    189.  
    190.     if (doType == 1) {
    191.      
    192.         //spilleren har ikke medbragt det rigtige objekt
    193.         if (playerHasObject == 0) {
    194.             triggerCount++;    
    195.  
    196.         }
    197.    
    198.         //spilleren har medbragt det rigtige objekt
    199.         if (playerHasObject == 1) {
    200.             triggerCount++;
    201.             triggerCountWithObject++;      
    202.         }
    203.    
    204.         //spilleren har medbragt det rigtige objekt, men vi har det i forvejen (fejl ?)
    205.         if (playerHasObject == 2) {
    206.             triggerCount++;
    207.             triggerCountWithObject++;
    208.         }
    209.        
    210.         //spilleren har givet os det rigtige objekt
    211.         if (playerHasObject == 3) {
    212.             triggerCount++;
    213.             triggerCountWithObject++;
    214.         }
    215.        
    216.         //spilleren har klikket paa et inventory-items inspect knap
    217.         if (playerHasObject == 4) {
    218.             triggerCount++;
    219.             triggerCountWithObject++;
    220.         }
    221.            
    222.        
    223.     } else if (doType == 2) {
    224.         //state machine til budbringerens response
    225.         p_data.person_state = 1;
    226.     }
    227.    
    228.    
    229.     //Tjek om der skal bruges et array til at kalde
    230.     if (guiTexArrayActive == 1) {
    231.         print ("kalder showtex med array");
    232.         guitex_messagee.SendMessage (guitex_func, guiTexArray);
    233.     } else {
    234.         print ("kalder showtex med variabel");
    235.         guitex_messagee.SendMessage (guitex_func, guitexture);
    236.     }
    237.    
    238.     //lav textresponse uanset hvad
    239.     textresponse.doText(playerHasObject, objectName, triggerCount, triggerCountWithObject, guitexture);            
    240. }
    241.  
     
  4. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    Here is the script called SET_personproperties
    Code (csharp):
    1.  
    2. //dette script definerer properties for personer i spillet
    3.  
    4. //generelle stats
    5. var person_navn = "";
    6. var person_state = 0;
    7.  
    8. //ting som personen oensker sig og deres status
    9. var person_oensker = ["","","","",""];
    10. var person_oensker_status = [0,0,0,0,0];
    11.  
    12. //svarmuligheder hvis det er foerste gang spilleren snakker med personen
    13. var person_comment_intro = "";
    14.  
    15. //svarmuligheder hvis spilleren viser personen et objekt som personen kender
    16. var person_comment_objects = ["null","null","null","null","null","null","null","null"];
    17.  
    18. //liste over hvilke objekter de forskellige comments hoerer til
    19. var person_comment_objects_index = ["null","null","null","null","null","null","null","null"];
    20.  
    21. //svarmuligheder hvis spilleren viser personen et objekt som personen ikke kender
    22. var person_comment_unknown1 = "Det kender jeg ikke noget til";
    23. var person_comment_unknown2 = "Hvad er dog det?";
    24. var person_comment_unknown3 = "Saadan en ting har jeg aldrig set foer";
    25.  
    26. //svarmuligheder hvis spilleren giver os det onskede objekt
    27. var person_comment0 = "";
    28. var person_comment1 = "";
    29. var person_comment2 = "";
    30. var person_comment3 = "";
    31. var person_comment4 = "";
    32.  
    33. //tak for det
    34. var person_comment_thanks = "";
    35.  
    36. //random muligheder hvis man har snakket med personen foer
    37. var person_comment_random0 = "random 0";
    38. var person_comment_random1 = "random 1";
    39. var person_comment_random2 = "random 2";
    40.  
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I edited the above posts to surround the code snippets inside
    Code (csharp):
    1.  tags -- makes them easier to read, as it preserves the indentation.
     
  6. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    thanks, I didn't know you could do that :)
     
  7. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    alright, I think I'm a little further along now...

    I tried adding a print-statement and commenting all code which references the p_data variable, to check whether or not I could access anything from the SET_personproperties script:

    Code (csharp):
    1.    
    2. //connect til script der indeholder personens properties
    3.     var p_data : SET_personproperties;
    4.     p_data = GetComponent (SET_personproperties);
    5.     print (p_data.person_navn);
    6.  
    Turns out it works like a charm. But if I try uncommenting this code:

    Code (csharp):
    1.            
    2. var minCheck = 0;
    3.        
    4.             for (i=0;i<=4;i++) {
    5.                 if (p_data.person_oensker[i] == other.name) {
    6.                     minCheck++;
    7.                 } else {
    8.                 }
    9.             }
    I get the "Unknown identifier : 'p_data'" error.

    I'm guessing this may be because Unitys array-handling has changed in 1.5.1 (since p_data.person_oensker is an array), but what do I do to correct this?
     
  8. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    I figured it out :eek:!

    Turns out that my getComponent code:

    Code (csharp):
    1.     var p_data : SET_personproperties;
    2.     p_data = GetComponent (SET_personproperties);
    3.  
    has to be inside every function in order for SET_personproperties to be accessible. I had to manually paste them into all my functions before it worked. Seems a bit odd though... I get a lot of extra code to work with and there doesn't seem to be any reason that it should work this way.
     
  9. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    also, I'd imagine that there is a hefty performance hit for using getcomponent in an onTriggerStay-function...

    any unity-developers around :roll: ?
     
  10. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I was typing up my response, really a question about scope when you responded. You response answered my question.


    You can place
    Code (csharp):
    1.  
    2. var p_data : SET_personproperties;
    3.  
    at the top of your script so it does not have to declared in every function. Then in Awake() or Start() you can get the instance:
    Code (csharp):
    1.  
    2. function Start()
    3. {
    4.   p_data = GetComponent (SET_personproperties);
    5. }
    6.  
     
  11. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You do not need to put the GetComponent into all functions. Just make sure the variable is visble outside the if-block by moving the declaration (the var p_data : SET_personproperties; line) above the block.
    Every time you have code inside {...}s you introduce a scope. Variables declared inside the block do not exist outside.

    Example:
    Code (csharp):
    1.  
    2. var a = 1;
    3. if (a == 1) {  // New scope
    4.     var b = 2;
    5.     print(a); // OK
    6.     print(b); // OK
    7.     for ( var t in range(1,2) )  {  // Sub-scope
    8.        var c=3;
    9.        print (c) ; // OK
    10.        print(a); // OK - variables declared outside are visible inside
    11.        print(b); // OK
    12.     } // c is no longer valid
    13.     print(c); // ERROR c is an unknown identifier.
    14.     print(b) ; // still OK
    15. } // now b is not valid
    16. print (b); // ERROR b is unknown
    17. print (a); // OK
    18. print (c); // ERROR: c? What c? never saw any c :P
    19.  
     
  12. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Thank you freyr for that clarification. If you have a statement outside any functions in a Javascript script, is that much different than having it in Start()? Won't it be evaluated when the script is first created? Is it evaluated before any function calls?
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It's essentially the same as placing it in the Start function. It's usually preferrable to keep it in the Start function since that is more readable.
     
  14. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Joachim, thank you for the clarification.
     
  15. peckinpah

    peckinpah

    Joined:
    Mar 15, 2006
    Posts:
    71
    Thanks for the explanations, it makes perfect sense now -but why did my original code work before in 1.5??

    I mean, I'm sure its just something I missed, but it just seems really odd...
     
  16. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I think I remember there was a bug in the JavaScript compiler that did not create a new scope for variables declared inside if-blocks. So code like this
    Code (csharp):
    1.  
    2. if( ... ) {
    3.     var a;
    4.     ...
    5. }
    6.  
    Got compiled as if it was:
    Code (csharp):
    1.  
    2. var a;
    3. if( ... ) {
    4.     ...
    5. }
    6.