unity 3d student

Beginner B04 – Destroying Objects


How to remove objects from scenes using the Destroy command.

Code Used

//Simple Destroy Command in Start Function
  1.  
  2. function Start () {
  3.  Destroy(gameObject.Find("Box"), 3);
  4. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

18 Comments

    Here’s the code for developers using C#:

    // Use this for initialization
    void Start () {
    Destroy( GameObject.Find( “Box” ), 3 );
    }

    Only issue to take care of is that the Find() method is a static function, so you have to call it with the class (GameObject) instead of the object(gameObject). So if you get error CS0176 you know what went wrong 😉

    Thanks for the great tutorials, they are really nice to watch and learn from.

  • Even though this lesson is about destroying objects, I think that the really interesting part is using scripts to apply commands to different objects.

  • but what should i do to destroy prefabs, or all objects with same name?

    • well, i tried Update method instead Start, objects vanished each 3 seconds 🙂

    scrn, i am by no means even an amateur (is that spelled right?) but i would add a tag to your objects and destroy objects with the same tag.

    not sure how though

  • These tutorials are really terrific. Your demonstrations are so clearly and effectively put! I was messing around with the code introduced in this chapter and I have a question…

    I tried to use “FindWithTag” in attempt to destroy four objects in my level with the tag “Monster”. I put the Destroy(gameObject.FindWithTag(“Monster”)) in the Start() function, but only one of the four objects tagged “Monster” was destroyed. I noticed this was the last object I created with the tag “Monster”, but other than that I don’t know why it isn’t destroying all the objects tagged “Monster”. Any advice?

  • I combined this tutorial with the previous ones by using an object to block my bouncybox prefabs from falling. Once that object was destroyed, the cubes dropped freely.

  • help me please..
    i cant use “destroy” with unity 3.4 , but if i use unity 3.3 i can
    whats wrong with new version..sorry for my bad english

  • Please help me, i try to type Destroy(“gameObject”); but i still have error said (no appropriate version of ‘UnityEngine.Object.Destroy’ for the argument list ‘(System.Type)’ was found )

    Please help me 🙁

  • I am Student, And i don’t know how can i collider detection, and script. Currently i place this Java Script. But it’s not properly working in my coin object.

    function OnTriggerEnter( other : Collider ) {
    Debug.Log(“OnTriggerEnter() was called”);
    if (other.tag == “Coin”) {
    Debug.Log(“Other object is a coin”);
    // score += 5;
    //Debug.Log(“Score is now ” + score);
    Destroy(other.gameObject);
    }
    }

    So How could I destroy a coin having sphere collider using mecanim character otherwise i put on other collider.

  • What should I write if I want a ball to destroy an object when it hits it?

  • Hey, was just wondering, I’m in a class currently learning Unity and such, and for an exercise we’re supposed to destroy one of our 3d objects we made last week. I made a crate, and placed several of them around the map, and I was thinking I (as the player) could walk around, left click next to a crate, and destroy it, what would be the best way of doing that?

  • Hi thank you for the tutorials. I have a looong way to go, but you make it seem possible somehow, so thank you.

    I have a question – I copied your code exactly at about 1.00 in the video, yet it throws a compiler error for “Destroy(gameObject);”. Using Unity 4.2.2, the Destroy function doesn`t seem to exist as a function in the scriptwriting programme. Has it been replaced?

    • No, definitely not, nothing has changed about Destroy, so you must have missed something further up your code most likely, meaning the error isn’t actually with Destroy but only causes an issue when you get to that line.

    Ha ha! I used this code in combination with the code from lesson 01 to destroy my cube when they impact the ground! Thank you so much for making me feel smart and for creating all these great lessons! I can’t wait to continue working on these!

Leave a Comment