- Home /
How can i destroy a gameobject?
When I destroy a gameobject instead of it disappearing it just returns to origin (and disappears from the hierarchy window)
test gameobject i made is destroyed no problems
code is: void OnTriggerEnter (Collider col) { if (col.gameObject.tag == "Bullet")
{
Destroy(gameObject);
}
}
it works but I don't know why it's returning the gameobject to origin - any thoughts?
NB: Here is a video on youtube:
The blue cube is the test gameobject and has all the same properties as the zombie head as well as the same code
update:
I was told everything is working ok but because the zombie in animated it works different from the other 2 and afte I attached the script to the parent of the head it did in fact work destroy the entire zombie (which i don't want)
It was recommended i swap to set active so ive changed it from destroy to SetActive
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Bullet")
{
Debug.Log ("Good shot!");
gameObject.SetActive (false);
This but this deactivates box collider (set to trigger) but not head - any suggestions?
Answer by LeonhardP · Feb 14, 2018 at 03:49 PM
The code you provided here should work, but that alone is not enough information to go on to be able to tell if this is a bug or a user error. Please submit a bug report with a reproduction project attached. You can find information about bug reporting here: https://unity3d.com/unity/qa/bug-reporting
Answer by · Feb 14, 2018 at 07:15 PM
If you want to disable head visually use col.gameObject.GetComponent<MeshRenderer>()
, to be sure you reference desired GameObject use col.gameObject.GetComponent<GameObject>()