Search Unity

Health and damage

Discussion in 'Scripting' started by GhostFire, Mar 25, 2010.

  1. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    Hi,

    i want to add health to an object,so i have added these two variables

    Code (csharp):
    1.  
    2.  
    3. var health = 10;
    4. var Dead : AudioClip;
    5.  
    6.  
    now,with the cannon gun script of my tank i want to subtract a value from the health script and then,when the value is of 0 start the Dead var.(When a particle hit a model it must subtract tot value from him)



    how can i do?
     
  2. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    Attach a collider to the object that you want damageable and use the OnCollisionEnter function to detect when something hits it. If the object hitting it is a projectile (or whatever it is that causes damage), decrement the health and check if that value has hit zero.

    Code (csharp):
    1. var health = 10;
    2. var dead : AudioClip;
    3.  
    4. function OnCollisionEnter (collision : Collision) {
    5.     // Make sure only collisions with weapons cause damage
    6.     if (collision.gameObject.name == "Bullet (Clone)") {
    7.         health -= 1;
    8.        
    9.         if (health <= 0) {
    10.             Die();
    11.         }
    12.     }
    13. }
    14.  
    15. function Die () {
    16.     audio.PlayOneShot(dead);
    17. }
     
  3. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    Hi,

    thank you for the help but i can not make it to work,

    i have renamed Bullet (clone) with the name of my particle,a damage value of 5 and when i it the object with the particle 2 times it doesn't do anything.Also i have added a box collider to the object.

    I think,particle don't collide with the collider box,there is a function for write in the log a word for test if it collide?
     
  4. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    someone can help me?... :(
     
  5. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    for the Bullet (Clone) line, replace it with YourProjectilesName (Clone). It's important to add the (Clone) part. Also make sure you have a box collider on your projectile.
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can use the print statement to write a message to the log:-
    Code (csharp):
    1. print("This bit gets executed");
     
  7. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    Mm i'm tryng to see if it collide with this code:


    Code (csharp):
    1. var health = 10;
    2. var dead : AudioClip;
    3.  
    4. function OnCollisionEnter (collision : Collision) {
    5.    // Make sure only collisions with weapons cause damage
    6.    if (collision.gameObject.name == "Small explosion(clone)") {
    7. print("ciao");
    8. }
    9.  
    10. }
    11.  
    12.  
    but it won't write "ciao" on the console...i have also added a box collider to Small explosion with the X,Y,Z size of 5 (also added a box collider to the object who i want to hit)
     
  8. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Are you sure the name is correct, are capitals and stuff correct like "Small Explosion (Clone)" instead of "Small explosion(clone)". I really don't know if it makes a difference. An easier way to do it is to just check the tag instead and tag it "Small Explosion" that way you don't need to worry about the (Clone) part.
     
  9. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    Ok,particle collides but now it says me this error:

    Code (csharp):
    1. "MissingComponentException: There is no 'AudioSource' attached to the "node_Box03" game object, but a script is trying to access it.
    2. You probably need to add a AudioSource to the game object "node_Box03". Or your script needs to check if the component is attached before using it."
    i have added an audio source to node_Box03 but i cant hear anything
     
  10. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Did you drag the audio clip on to the variable dead in the inspector? and did you play it using PlayOneShot?
     
  11. GhostFire

    GhostFire

    Joined:
    Mar 2, 2010
    Posts:
    29
    yes
     
  12. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    I am getting the same problem.
     
  13. Spykam222

    Spykam222

    Joined:
    Mar 14, 2010
    Posts:
    2
    Ok, lets make this simple.

    1. Create a GameObject.
    2. Create a Java Script file.
    3. Copy the Code and paste it right at the top of the code.
    4.Drag the java script file to the game object.
    5. and done

    -----------------
    Unity Pro