Search Unity

Spot the difference

Discussion in 'Scripting' started by AaronC, Dec 12, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    This would be good to understand......?
    -Sorry the capitalisation is hard to read, its easier to read not as code




    function OnTriggerEnter (col: Collider){}

    // vs

    function OnTriggerEnter (Col: Collider){}

    // capital C

    function OnTriggerEnter (collider: Collider){}

    //collider instead of col

    function OnTriggerEnter (collision: Collision){}

    //colision instead of Collision

    function OnTriggerEnter (){}

    //well nothing...?[/code]
    Thank you very much... :idea:
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    The first four are the same. In JavaScript, the type of the variable follows the variable name. So here, the type is Collider and the name of the variable is whatever you like.

    You just as easily have:

    function OnTriggerEnter (MagicalPonies : Collider){}
     
  3. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Is this what you mean?
    Code (csharp):
    1.  
    2. var MagicPonies : GameObject;
    3. function OnTriggerEnter (MagicalPonies : Collider){
    4. //MagicPonies.renderer.enabled = true;
    5. }
    6.  
    So I could do this (as an example)?

    Surely theyre not all the same...?

    EDIT-Oh I get it-But what about the bottom2?
    thanks
    AC
     
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    To clarify, you don't need the variable declaration in the first line. In JavaScript, anytime you see a ":" after a variable name you're declaring a new variable. Function signatures just don't use the "var" keyword, that's the only difference.

    So all you need is:

    Code (csharp):
    1.  
    2. function OnTriggerEnter (MagicPonies : Collider) {
    3.  MagicPonies.renderer.enabled = true;
    4. }
    5.  
    What you're saying there is that you're declaring a new "MagicPonies" variable for use inside that function, of type Collider, and whatever's calling that function will provide the data to fill the variable.

    On the last function example in your original post--that's basically the same function, except a version where the game engine doesn't pass you the object that entered the trigger. You'd use that when you care that something has entered the trigger, but you don't care which something.

    Hope that helps!
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Cool -Thanks
    AC