- Home /
How would I destroy the gameobject that a raycast hit? (SOLVED)
I was making something that would destroy a gameobject when a raycast hit it, but the object that the ray hit is not a gameobject. It is a RaycastHit. How would I destroy the RaycastHit or maybe even convert the RaycastHit into a gameobject?
Answer by robertbu · Apr 20, 2014 at 02:09 AM
Assuming your RaycastHit variable is named 'hit', you could do:
Destroy(hit.collider.gameObject);
You can get access to the game object from any component, so you could also use:
Destroy(hit.transform.gameObject);
or if your game object has a rigidbody:
Destroy(hit.rigidbody.gameObject);
Your answer
![](https://koobas.hobune.stream/wayback/20220613143125im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Help destroying gameObject on RayCastHit? 1 Answer
need help to destroy game object 1 Answer
Detect Object that are hit by a ray 3 Answers
Object reference not set to an instance of an object (with a RaycastHit) 3 Answers