Search Unity

modify pickup script

Discussion in 'Scripting' started by Velketor, Oct 24, 2007.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    I'm using a pickup script from the third person sample pack that's on the unity's community site. I suck at javascript, and I'm trying to add a pickup type, but I just don't know where to even begin. Here's the code I'm working with:
    Code (csharp):
    1.  
    2. enum PickupType { Health = 0, ExtraLife = 1 }
    3. var pickupType = PickupType.Health;
    4. var amount = 1;
    5. var sound : AudioClip;
    6.  
    7. private var used = false;
    8.  
    9. function ApplyPickup (health : ThirdPersonHealth)
    10. {
    11.     if (pickupType == PickupType.Health)
    12.     {
    13.         if (health.health >= health.maxHealth)
    14.             return false;
    15.        
    16.         health.AddHealth(amount);
    17.     }
    18.     else if (pickupType == PickupType.ExtraLife)
    19.     {
    20.         health.AddLife(amount);
    21.     }
    22.    
    23.     return true;
    24. }
    25.  
    26. function OnTriggerEnter (col : Collider) {
    27.     var health : ThirdPersonHealth = col.GetComponent(ThirdPersonHealth);
    28.    
    29.     //* Make sure we are running into a player
    30.     //* prevent picking up the trigger twice, because destruction
    31.     //  might be delayed until the animation has finnished
    32.     if (used || health == null)
    33.         return;
    34.    
    35.     if (!ApplyPickup (health))
    36.         return;
    37.     used = true;
    38.    
    39.     // Play sound
    40.     if (sound)
    41.         AudioSource.PlayClipAtPoint(sound, transform.position);
    42.    
    43.     // If there is an animation attached.
    44.     // Play it.
    45.     if (animation  animation.clip)
    46.     {
    47.         animation.Play();
    48.         Destroy(gameObject, animation.clip.length);
    49.     }
    50.     else
    51.     {
    52.         Destroy(gameObject);
    53.     }
    54. }
    55.  
    56. // Auto setup the pickup
    57. function Reset ()
    58. {
    59.     if (collider == null)  
    60.         gameObject.AddComponent(BoxCollider);
    61.     collider.isTrigger = true;
    62. }
    63.  
    64. @script AddComponentMenu("Third Person Props/Pickup")
    65.  
    Any help is appreciated.
     
  2. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    I modified it a bit, but what i have doesn't work. It's giving me an error saying "an instance of type 'ScoreGood' is required to access the non static member 'addtoscore.'" ScoreGood is a script I have that's just to add to the score variable, and addtoscore is the function. Here's the code:
    Code (csharp):
    1.  
    2. enum PickupType { Health = 0, ExtraLife = 1, Score = 2 }
    3. var pickupType = PickupType.Health;
    4. var amount = 1;
    5. var sound : AudioClip;
    6.  
    7. private var used = false;
    8.  
    9. function ApplyPickup (health : ThirdPersonHealth)
    10. {
    11.     if (pickupType == PickupType.Health)
    12.     {
    13.         if (health.health >= health.maxHealth)
    14.             return false;
    15.        
    16.         health.AddHealth(amount);
    17.     }
    18.     else if (pickupType == PickupType.ExtraLife)
    19.     {
    20.         health.AddLife(amount);
    21.     }
    22.     else if (pickupType == PickupType.Score)
    23.     {
    24.         ScoreGood.AddToScore  () ;
    25.     }
    26.    
    27.     return true;
    28. }
    Everything afterwards is the exact same as the script from my first post. I just don't know why it's giving me this error, any ideas? Once again, any help is appreciated. Thanks[/code]
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    At the risk of basically repeating that message, you need an instance of 'ScoreGood' to access 'addtoscore'. :) Or to put it a slightly different way, you need a reference to 'ScoreGood' somehow. You can do it a number of ways; here's one:

    Code (csharp):
    1. var scoreScript = FindObjectOfType(ScoreGood);
    2. scoreScript.AddToScore  () ;
    FindObjectOfType returns the first object it finds of that type; presumably you only have one instance of that script, so that's OK.

    --Eric