- Home /
The question is answered, right answer was accepted
action to click the left mouse
without using the raycast would like to achieve something like this: When I click the left button of the mouse on a GameObject I would like something to happen, for example, I click on a cube, and it disappears. What is the script that I can accomplish this? possibly in C #
Answer by robertbu · Dec 01, 2013 at 04:01 PM
You are looking for OnMouseDown():
using UnityEngine;
using System.Collections;
public class Click : MonoBehaviour {
void OnMouseDown() {
renderer.enabled = false;
}
}
This works for all GameObject, but how do I get it to work only with those who have a particular name?
You had to create a public variable and assign the GameObject to the inpector, and later call the named variable por example (public GameObject cube) and On$$anonymous$$ouseDown cube.enabled=false;
@Ikillnukes - No, you don't need public game object as you describe. The code above works fine, and does what he requested. Put this scrip on a game object, run the app. When you click on the game object, it will disappears. Your suggested change and your code below would only be necessary if you want some other game object to disappear. That is you click on cube1 and it makes cube2 disappear.
Just use what @robertbu showed you and check the name lol.
using UnityEngine;
using System.Collections;
public class Click : $$anonymous$$onoBehaviour
{
void On$$anonymous$$ouseDown()
{
if(name == "Cube")
renderer.enabled = false;
}
}