x


mouse click on gameobject!!

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.

more ▼

asked Dec 02 '10 at 11:54 PM

tomeromero gravatar image

tomeromero
75 15 15 22

(comments are locked)
10|3000 characters needed characters left

5 answers: sort newest

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.

more ▼

answered Dec 03 '10 at 12:03 AM

Jesus_Freak gravatar image

Jesus_Freak
1.2k 38 44 59

But that will destroy the game object... Not what he clicks

Dec 03 '10 at 12:15 AM AVividLight

@GBStudios: obviously the script goes on the object, otherwise OnMouseDown wouldn't work. So it does destroy what you click.

Dec 03 '10 at 12:17 AM Eric5h5

Yeah, but, I was under the impression that he want to click anything... Sorry if I wasn't clear.

Dec 03 '10 at 03:54 AM AVividLight

Thank you guys..!

Dec 03 '10 at 12:54 PM tomeromero
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Aug 20 '12 at 11:55 AM

Kazemc gravatar image

Kazemc
-4

(comments are locked)
10|3000 characters needed characters left

Rather than destroying the game object, just load a scene. For example:

Application.LoadLevel("Scene");

more ▼

answered Jun 15 '12 at 06:19 PM

poseur976 gravatar image

poseur976
-5

(comments are locked)
10|3000 characters needed characters left

can you do the same but instead of destroying the game object it changes to another scene?

more ▼

answered Apr 28 '12 at 12:06 PM

Kiffster11 gravatar image

Kiffster11
-10

Rather than destroy the object, just load the scene. For example:

Application.LoadLevel("Scene");

Jun 15 '12 at 06:17 PM poseur976
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 03 '10 at 12:14 AM

AVividLight gravatar image

AVividLight
1.9k 68 100 129

Thank you for the script.

Dec 03 '10 at 12:53 PM tomeromero

Sure, hope I helped!

Dec 03 '10 at 04:15 PM AVividLight
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5272
x3570
x3419

asked: Dec 02 '10 at 11:54 PM

Seen: 12632 times

Last Updated: Aug 20 '12 at 11:55 AM