Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kyogretcg · Aug 19, 2013 at 06:18 PM · how manysending object

How to destroy a gameobject when I collide with it?

I know how to destroy a game object but I want the object to be destroyed when my player which is a lawn mower collides with the grass. The mower name is "LawnMower" and the grass is named "grass_clump_04". If somebody could please help me with this aswell as how do I make the game go to the ending screen after I destroyed all the grass objects. Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Aug 19, 2013 at 06:27 PM

Something like this attached to the object you want to destroy?

 function OnCollisionEnter(col : Collision)
     {
         if(col.collider.gameObject.name == "LawnMower")
         {
             Destroy(gameObject);
         }
         else if (col.collider.gameObject.name != "LawnMower")
             {
                     //Something else
             } 
     }


End level, something like this attached to the Player:

 var grassObjects : GameObject[];
 
 function Update()
 {
      grassObjects = FindGameObjectsWithTag ("Grass");
      var count = grassObjects.length;
      if ( grassObjects == 0 )
      {
           //End level by showing end screen or loading next etc
           //Debug.Log("No Grass Left");
           //Application.LoadLevel ("NextLevel");
      }
 }

Make sure you tag your grass as "Grass" in the inspector. Add a tag by increasing the Elements by one and adding it in the new empty field.

You probably will want to do this periodically rather than every frame to avoid chewing up your cpu cycles. Find functions are very expensive. Try looking in to this:

Unity invoke repeating

Its all untested, I could be wrong. Let me know if it throws up errors :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Aug 19, 2013 at 06:38 PM 0
Share

Post your code, lets have a look.

avatar image kyogretcg · Aug 19, 2013 at 07:26 PM 0
Share

I tried the end level but it didnt work which I'm sure is my error. I'm new to scripting so I dont really know how any of this works or what to apply things to.

avatar image meat5000 ♦ · Aug 21, 2013 at 08:27 AM 0
Share

Edited code. try the new one

avatar image
0

Answer by kyogretcg · Aug 19, 2013 at 06:34 PM

The grass gets destroyed but I havent tested the end level.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flaviusxvii · Aug 19, 2013 at 06:40 PM 0
Share

I think you were trying to post a comment to meat5000s answer. Slow down. Read the buttons.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help to code line of bullet like gunbound ! 1 Answer

Help please want to reset game 0 Answers

Need to press key twice to react 1 Answer

How to detect collision of a character in diffrent body parts 0 Answers

Coding Problems, Ok new to this system and coding coming from Visual Communication we did some coding but not much, 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges