Just as the ultra-noob question title states. I'm sure there is a simple script, but I can't find it. How can I make my gameobject cube disappear when I click it?
3 Answers
3Attach a simple collider and this script to the object. Choose one of the options.
function OnMouseDown () {
gameObject.active = false;
//Destroy(gameObject);
//renderer.enabled = false;
}
Check out this link about raycasting. This will make your object clickable:
http://unity3d.com/support/documentation/ScriptReference/Collider.Raycast.html
You'll then want to enable or disable the renderer if the raycast hits something
Here's the link for that:
http://unity3d.com/support/documentation/ScriptReference/Renderer-enabled.html
Maybe something like this?:
var objToDel : GameObject; <--- But "cube" here
function Update()
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 100.0))
{
delta = hit.gameObject.name;
}
if (delta == objToDel.gameObject.name)
{
objToDel.active = false;
}
}
Not quite sure, can't verify, not @ my computer.
this answer is much less effort than raycasting :)
– anon61858128Interesting. Haven't used this before. Looks like this function is a shorthand way of doing the raycast for you. In the scripting reference it says that this won't work on Ignore Raycast layers. It also says it doesn't work on iPhone apps. But otherwise seems like a good way of handling mouse click events. Thanks!
– tool55Just as an added thought, OnMouseDown() doesn't seem to give you control of depth (z axis) so you'd have to do your own raycast if you only want objects closer than a certain distance to be clickable.
– tool55