Hi...
I have a gameobject(cube) in my scene.
I want to be able to click on the gameobject and automaticaly destroy the gameobject so i wont be able to see it on my screen.
What script i have to attache on my gameobject??
Thank you.
Hi...
I have a gameobject(cube) in my scene.
I want to be able to click on the gameobject and automaticaly destroy the gameobject so i wont be able to see it on my screen.
What script i have to attache on my gameobject??
Thank you.
the cube should have a collider, and the script i thionk you want is when you click on the cube, it's destroyed forever. until you play the scene again.
here it is:
function OnMouseDown()
{
Destroy(gameObject);
}
that should work.
right! also, this script goes on the object(s) you want to destory.
Tomeromero,
This script will destroy the gameobject when you click:
function Update () {
if(Input.GetMouseButton(0))
Destroy (gameObject);}
And this script, will give you an idea of how to do what you asked... (I am not posting this whole script, so you can learn, and answer these questions yourself)
// Public member variables that you can change in the Inspector
public var distance : float = 5.0;
function Update()
{
Debug.DrawRay(transform.position, transform.forward * distance, Color.white);
// Shoot the ray on mouse click
if (Input.GetButtonUp("Fire1"))
{
var hit : RaycastHit;
// Cast a ray forward from our position for the specified distance
if (Physics.Raycast(transform.position, transform.forward, hit, distance))
{
// Add a specified force to the Rigidbody component to the other GameObject
Debug.Log("Destroyed " + hit.collider.gameObject.name);
try
{
hit.collider.gameObject.Destroy;
} catch (e) { }
}
}
}
-Hope I helped
can you do the same but instead of destroying the game object it changes to another scene?
OnMouseDown() is better.
An important point to make is that OnMouseDown only works on GUIElements or Colliders, so if your game object doesn’t have a collider or isn’t a GUIElement, it won’t work.
Hi,
I’m facing a similar issue, I want to make rotate one image in the middle of 25 others. @Whalefly recommended me to use this:
void OnMouseDown()
{
transform.Rotate(new Vector3(0f, 0f, 90f));
}
But it is not working. Have you any idea? (The aim is to create a puzzle where the player has to turn the images to put them in the correct rotation).
Rather than destroying the game object, just load a scene. For example:
Application.LoadLevel(“Scene”);
Hey!
I think this URL can help you
Hope i helped