Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destroying an Object

Discussion in 'Scripting' started by marvinhawkins, Nov 2, 2009.

  1. marvinhawkins

    marvinhawkins

    Joined:
    Oct 3, 2009
    Posts:
    55
    I'm trying to destroy a game object from the RTS script. I have the script setting up a collision and printing the text "I got hit" when it takes damage.
    The part of the script that takes away "health" also works


    The next line, Destroy(GameObject) Does not. I'm wondering why this is the case? What am I doing wrong?


    Also if there's any insight on where I can go from here? For example the player's units can collide and be destroyed by anything idealy the script should check to see what the tag of the colliding object would be and then handle the rest. IE if its a bullet then do damage, if its a building or another unit, it shouldn't.

    Code (csharp):
    1.  
    2.  
    3.  
    4. function OnCollisionEnter ()
    5. {
    6.     print("I Got Hit!..");
    7.    
    8.    
    9.     Destroy (rigidbody);
    10.     Destroy (gameObject);
    11.      
    12.    
    13.     /*health = health - 100;
    14.    
    15.     if  (health  >= 0)
    16.     {  
    17.         print(" I'm Taking Dead");
    18.        
    19.        
    20.     //  Destroy (gameObject);
    21.        
    22.         //DestroyObject(this);
    23.        
    24.        
    25.     }*/
    26.    
    27.    
    28. }
    29.  
    Sorry for the long post, and the newbish question but I'm stumped and I appreciate any help you can provide.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I'm not sure if the Destroy call on the rigidbody is causing problems here. In any case it is unnecessary - the rigidbody will be destroyed along with the GameObject, assuming you can make that happen...

    As regards the other question, OnCollisionEnter receives a parameter of type Collision. This class contains information about the object that was hit (the transform, etc).
     
  3. marvinhawkins

    marvinhawkins

    Joined:
    Oct 3, 2009
    Posts:
    55
    thanks Andeee,

    but what's going wrong when the object doesn't get destroyed? Not the Rigidbody. I added a Destroy (gameObject) and it does nothing. where did I go wrong?
     
  4. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    try:

    Destroy(this.gameObject);

    ?
     
  5. helioraanswer

    helioraanswer

    Joined:
    Apr 7, 2009
    Posts:
    412
    Hi, I have a destroy object with Hit Points, but can't get the animation right.

    Code (csharp):
    1.  
    2. var maximumHitPoints = 3.0;
    3. var hitPoints = 3.0;
    4. var deathtimer = 0.0;
    5.  
    6. function Start ()
    7. {
    8.     animation.wrapMode = WrapMode.Loop;
    9.     animation["destruction"].wrapMode = WrapMode.Once;
    10.  
    11.     animation["idle"].layer = 2;
    12.     animation["destruction"].layer = 3;
    13.  
    14.    
    15.     //animation.Stop();
    16.     animation.Play("idle");
    17. }
    18.  
    19. function OnCollisionEnter (collision : Collision) {
    20. //  print(collision.gameObject.name); debug
    21.     if((collision.gameObject.name == "vatombullet(Clone)") || (collision.gameObject.name == "Soldat Slice(Clone)"))  {
    22.  
    23.  
    24.         // And kill our selves
    25.         hitPoints --;
    26.        
    27.         if (hitPoints < 0.0) {
    28.             Kill();
    29.  
    30.         }
    31.     }
    32. }
    33.  
    34. function Kill () {
    35.     // Destroy the projectile
    36.     deathtimer ++;
    37.     animation.CrossFade("destruction");
    38.     if (deathtimer > 5.0){
    39.     Destroy(gameObject);
    40.     }
    41. }
    42.  
    Anyone know what I'm doing wrong? If the HP is lower then 1 the timer should start to work to let the death animation play out and then destroy the object.
    But I have to shoot my target at least 5 times, and then every time I shoot it it plays a fraction of the animation. After 12 shots it finally is dead, but not the way I intended.

    Any help appreciated!

    thanks,