Hello everybody,
I have a prefab which is instantiated via GameObject.Instantiate… each prefab has a script attached which is in charge of detecting the hit of the mouse with the rigidbody and once there is a collition with the “selected” object I press “DELETE” to cancel the object from the scene.
It’s weird tho, that when I call the Destroy(hit.rigidbody.gameobject) I get a “null reference…” error message 
I don’t quite get why. Moreover, if the script is attached to the object which I want to cancel, how can I destroy directly that gameobject?
Many thanks.
This is my code:
function Object_delete() {
var hit : RaycastHit;
var e: Event = Event.current;
if (!Physics.Raycast(camera.main.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
if (hit.rigidbody == gameObject.GetComponent(Rigidbody)){
Debug.Log("OUCH YOU HIT ME!!!" );
if(e.isKey e.keyCode == KeyCode.Delete )
Destroy(hit.rigidbody.gameObject);
}
Debug.Log("Object_delete");
}
Where does Object_delete() get called from?
Also, it looks like there might be some logic errors in your code. Try cleaning it up a bit and making the indentation consistent. Then, make sure to use curly brackets after every conditional and loop statement (it’s not a language requirement, but it’s good practice and will make your code more clear). If after that you haven’t fixed the problem, post your revised code.
is that code attached TO the rigidbody that is being deleted when hit? if it is thats your problem, the ray is coming from a screencast to ray, and has to be attached to the camera.
instead of calling for the ray within the script attached to the rigid body you have to call to the script that is calling the ray, then pick up the hit
for example :
function Update(){
if( screen_cast_test.hit.transform.position == transform.position)
{
Destroy(gameObject);
}
}
in that script it is the if statement is calling for the variable “hit” in another script called screen_cast_test
if the position of that hit == the position of the gameobject with this destroy script on it, it will be deleted
hope it works for you
EDIT:
In my case i just had a collider and not a rigidbody, you may have to alter or add a collider.
Hi Pat,
yes I attached that script to the objects and not to the camera… as I thought that I can detect the raycast no matter the script is attached to. So, if my understanding is right, I should attach the script to the camera…and call the Destroy(gameobject) from the camera-script.
sorry to ask you again but what you mean with “instead of calling for the ray within the script attached to the rigid body you have to call to the script that is calling the ray, then pick up the hit”, it’s not clear to me
GC
there are a few steps you have to take
1- attach a script to camera that calls for a screenpoint to ray (like you did)
2- make your hit variable static so it can be called on from another script ( “static var hit : RayCastHit;”)
3- make another script and attach it to the object you wish to be deleted when hit with the ray
4- use my script from above but where it says “screen_cast_test” change that to the name of the script that is attached to the camera that casts the ray
let me know if that helps