Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroy on collision

Discussion in 'Scripting' started by AcidDreamer, Apr 9, 2013.

  1. AcidDreamer

    AcidDreamer

    Joined:
    Mar 22, 2013
    Posts:
    23
    I know many threads like these were made , but I cant seem to make it work

    So this script will be attached to an object which is destroyed after touching the "Player"

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchDestroy : MonoBehaviour
    5. {
    6.  
    7.  
    8. void OnCollisionEnter(Collision collision)
    9. {
    10.     if(collision.gameObject.name == "Player"){
    11.             Destroy(collision.gameObject);
    12.         }
    13.     }
    14. }
    15.  
     
  2. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Destroy(this);

    your are destroying the player not the object.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    *ahem* Destroy(gameObject);

    Destroy(this) would destroy the script from the gameObject (ie the script component).
     
    Simonlelegao likes this.
  4. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    lol ups ^^
     
  5. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    Code (csharp):
    1.   using UnityEngine;
    2.     using System.Collections;
    3.      
    4.     public class TouchDestroy : MonoBehaviour
    5.     {
    6.      
    7.      
    8.     void OnCollisionEnter(Collision collision)
    9.     {
    10.         if(collision.gameObject.name == "Player")
    11.        {
    12.                  Destroy(this.gameObject);
    13.             }
    14.         }
    15.     }
     
    Last edited: Apr 9, 2013
  6. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Did that script work? I'm not a c# guy cause it makes my eyes bleed. If it didn't work , then I wonder if you reference the object first.

    (eg)

    if(collision.gameObject.name == "Player")
    var Obj = collision.gameObject;
    Destroy(Obj);
     
  7. AcidDreamer

    AcidDreamer

    Joined:
    Mar 22, 2013
    Posts:
    23
    Nope it doesnt seem to work.. I added the script to a basic sphere for tests ..But on collision nothing happens..
    Maybe a public variable would make any difference?
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  9. AcidDreamer

    AcidDreamer

    Joined:
    Mar 22, 2013
    Posts:
    23
    That was the mistake , now its working ..Thank you very much all of yo1u