x


Destroy object on button push?

I want my player to destroy a cube when the down arrow key is pressed on a keyboard. I know I need to use the Destroy (Object); command but what would be the entire script?

more ▼

asked Aug 02 '12 at 03:25 PM

beefwellington gravatar image

beefwellington
-4 1 1

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

2 answers: sort voted first
class destroyObjectScript {

   void Update() {
     if(Input.GetKeyDown(valueForArrowDownKey) {
       Destroy(gameObject);
     }
   }

}

I would tell you to look up what the value for the Arrow down key is, but since it appears to be down, the IDE will probably help you auto-complete it.

more ▼

answered Aug 02 '12 at 03:42 PM

ChadM gravatar image

ChadM
208 4 8

I entered the script exactly as you've written it but it says there's an error. "Expecting }, got 'void'"

Aug 08 '12 at 01:02 PM beefwellington

You're using C# script and not Java script correct?

Aug 08 '12 at 01:16 PM ChadM

I'm using Javascript but when I created a C# script, I got a similar error message. If I wanted that script as a Javascript, what would it look like?

Aug 09 '12 at 04:22 AM beefwellington2

if you use the code exactly as it is, it don't can work

Replace (valueForArrowDownKey) with ("down"),

here the input value http://docs.unity3d.com/Documentation/Manual/Input.html

Aug 09 '12 at 04:34 AM Ingen

I tried that and got the same error: "Unexpected symbol: '{'"

I get similar errors and seem to be having the same problem with Javascript and C#

Aug 09 '12 at 04:41 AM beefwellington2
(comments are locked)
10|3000 characters needed characters left

thats what i get for copying the above text

class destroyObjectScript {

void Update() {

 if(Input.GetKeyDown(KeyCode.DownArrow)) {
   Destroy(gameObject);
 }

}

}

more ▼

answered Aug 09 '12 at 07:20 AM

avidgamer gravatar image

avidgamer
1 1

Missing a closing ) after input

if(Input.GetKeyDown(KeyCode.DownArrow))

Aug 09 '12 at 07:22 AM fafase
(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:

x1089
x763

asked: Aug 02 '12 at 03:25 PM

Seen: 338 times

Last Updated: Aug 09 '12 at 06:57 PM