- Home /
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"));
}
}
}
}
Answer by millej23 · Sep 06, 2016 at 04:42 AM
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!
Unfortunately, it's not what i'm looking for, i'm trying to destroy the dragger when the player lets go. Thanks for giving an interest though.
Answer by Acid_kenobi · Sep 05, 2016 at 10:33 PM
Pretty sure you could do:
if (dragger != null && Input.GetButtonUp("Fire1")){
Destroy(dragger );
dragger = null;
}
Sorry, its early. Hope this helps :)
unfortunately it didn't help, and it's weird because i even made it check before it destroys too. so i guess the problem has to be with the destruction part of it?
if(Input.GetButtonUp("Fire1"))
{
getDragger = GameObject.FindWithTag("dragger");
Destroy(getDragger);
}
Answer by tas41 · Sep 10, 2016 at 08:37 AM
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);
}
}
}
Answer by tqkiettk10 · Sep 10, 2016 at 03:51 AM
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"));
}
}
}
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer
Particle system not destroying. 3 Answers
How to only delete one of two collided objects? 1 Answer
Store Game Object Into List For Later Reinstantiation? 0 Answers