Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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,513
    Code (csharp):
    1.  
    2. Destroy(hit.transform.gameObject);
    3.  
     
  3. sinkler747

    sinkler747

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