Search Unity

destroy an object

Discussion in 'Scripting' started by sinkler747, Dec 10, 2012.

  1. sinkler747

    sinkler747

    Joined:
    Dec 10, 2012
    Posts:
    9
    hello, i'm trying to destroy a tagged object. i have tried many different ways. if i click on a tagged object, i want to destroy it. it seems simple but....... the only one that worked (somewhat) without any errors were Destroy(gameObject).. unfortunately, that was my player. thanks for any advice.






    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start ()
    4. {
    5.  
    6. }
    7.  
    8.  
    9. //var holdTagName = transform.tag("weapon");
    10. function Update ()
    11. {
    12.     if(Input.GetMouseButtonDown(1))
    13.     {
    14.        
    15.         var hit : RaycastHit;
    16.         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    17.         if(Physics.Raycast(ray,hit,4.0f))
    18.         {
    19.             if(hit.transform.tag == "weapon")
    20.             //if(other.gameObject.CompareTag("Player"))
    21.             {
    22.                 Destroy(transform.tag);
    23.                     //Destroy(transform.tag("weapon"));
    24.                  //Destroy (other.gameObject);//Destroy(//(holdTagName);
    25.                  // Destroy(gameObject);  no errors, but destroys player
    26.             }
    27.         }
    28.     }
    29.    
    30. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. Destroy(hit.transform.gameObject);
    3.  
     
  3. sinkler747

    sinkler747

    Joined:
    Dec 10, 2012
    Posts:
    9
    thanks KelsoMRK. that worked