C# destroy gameobject with tag

I’m making a script that creates a empty game object in the center of a rigid body with a specific tag, then destroys it when the mouse is lifted.
Everything but the destruction part seems to work fine, so what’s wrong?

GameObject dragger;
private Camera fpsCam;
public float range;

// Use this for initialization
void Start () {
    fpsCam = GetComponent<Camera>();

}

// Update is called once per frame
void Update () {

    if(Input.GetButtonDown ("Fire1"))
    {
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;
        if(Physics.Raycast (rayOrigin,fpsCam.transform.forward, out hit, range))
        {
            if (hit.transform.tag == "Grabbable")
            {
                Debug.Log("hit:" + hit.rigidbody);
                dragger = new GameObject("dragger");
                dragger.transform.position = hit.transform.position;
                dragger.transform.parent = fpsCam.transform;
                dragger.gameObject.tag = "dragger";
                
            }
            
        }
        else
        {
            Debug.Log("not hit");

        }                if (GameObject.FindWithTag ("dragger")&& Input.GetButtonUp("Fire1"))
                {
                Destroy(GameObject.FindWithTag("dragger"));

                }

             
                    
                }
    }
}

Looking at it quickly, try using:

if(Input.GetButton("Fire1"))

I believe the GetButtonDown will trigger once per press, while GetButton will trigger while the mouse is held down. If you are trying to check while holding the mouse or fire down, try GetButton to have the ray check while held down. Let me know if that helps, thanks!

Pretty sure you could do:

 if (dragger != null && Input.GetButtonUp("Fire1")){
    Destroy(dragger );
    dragger = null;
}

Sorry, its early. Hope this helps :slight_smile:

I see now, i think the problem was just a misplaced bracket, oops
Here’s the finished code :

using UnityEngine;
using System.Collections;

public class Raycast : MonoBehaviour {
GameObject dragger;
private Camera fpsCam;
public float range;
public GameObject getDragger;
// Use this for initialization
void Start () {
    fpsCam = GetComponent<Camera>();
}

// Update is called once per frame
void Update () {
    getDragger = GameObject.FindWithTag("dragger");
    if (Input.GetButtonDown("Fire1"))
    {
        Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));

        RaycastHit hit;

        if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, range))
        {

            if (hit.transform.tag == "Grabbable")
            {
                Debug.Log("hit:" + hit.rigidbody);
                dragger = new GameObject("dragger");
                dragger.transform.position = hit.transform.position;
                dragger.transform.parent = fpsCam.transform;
                dragger.gameObject.tag = "dragger";

            }

        }
        else
        {
            Debug.Log("not hit");

        }
    }         
        if(Input.GetButtonUp("Fire1"))
        {
            getDragger = GameObject.FindWithTag("dragger");
            Destroy(getDragger);
            
        }
             
                                       
    }
}

I think your block code “{}” is wrong
so I try to fix it:

 GameObject dragger;
     private Camera fpsCam;
     public float range;
     // Use this for initialization
     void Start () {
         fpsCam = GetComponent<Camera>();
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown ("Fire1"))
         {
             Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0));
             RaycastHit hit;
             if(Physics.Raycast (rayOrigin,fpsCam.transform.forward, out hit, range))
             {
                 if (hit.transform.tag == "Grabbable")
                 {
                     Debug.Log("hit:" + hit.rigidbody);
                     dragger = new GameObject("dragger");
                     dragger.transform.position = hit.transform.position;
                     dragger.transform.parent = fpsCam.transform;
                     dragger.gameObject.tag = "dragger";
                     
                 }else
             {
                 Debug.Log("not hit");
             } 
          }
                   if (GameObject.FindWithTag ("dragger")&& Input.GetButtonUp("Fire1"))
                     {
                     Destroy(GameObject.FindWithTag("dragger"));
                     }
         }
     }