- Home /
How can I make an object disappear when clicked?
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?
Answer by efge · Mar 26, 2011 at 03:22 PM
Attach a simple collider and this script to the object. Choose one of the options.
function OnMouseDown () {
gameObject.active = false;
//Destroy(gameObject);
//renderer.enabled = false;
}
Interesting. 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!
Just as an added thought, On$$anonymous$$ouseDown() 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.
Answer by tool55 · Mar 26, 2011 at 03:18 PM
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
Answer by Tommy · Mar 26, 2011 at 05:38 PM
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.
Your answer
Follow this Question
Related Questions
Sphere disappear after build 0 Answers
object dissappear and reappear 1 Answer
Player Moves Away = Object Disappear Scrpit - Help 1 Answer
Make Animated object disappear after it animates 1 Answer
Objects disappear after building 1 Answer