Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jammerjar · Jun 12, 2011 at 01:43 AM · collisiondestroycounter

Destroy object after 5 collisions

Hi this is my first post so go easy on me. I have searched but cant find info on a way to count the number of hits an object receives and and destroy it after it has been hit say 5 time.

My guess is that I need to count the number of collisions in a variable and then when the variable = 5 destroy object. But I can figure out how to do that. Oh yes and the object to destroy is an instance.

I have been trying to adapt this script that destroys after a period of time but no joy so thought i would put my hand up.

 // Instantiate a rigidbody then set the velocity
 
 var projectile : Rigidbody;
 var destroyTimeMin = 2.0;
 var destroyTimeMax = 5.0;
 var pop : ParticleEmitter;
 
 function OnCollisionEnter (col : Collision) {
   Instantiate(pop, transform.position, transform.rotation);
 }
 
 function Update () {
   // Ctrl was pressed, launch a projectile
   if (Input.GetButtonDown("Fire1")) {
     // Instantiate the projectile at the position and rotation of this transform
     var clone : Rigidbody;
     clone = Instantiate(projectile, transform.position, transform.rotation);
 
     // Give the cloned object an initial velocity along the current
     // object's Z axis
     clone.velocity = transform.TransformDirection (Vector3.forward * 10);    
 
     Destroy(clone.gameObject, Random.Range(destroyTimeMin, destroyTimeMax));
   }
 }
Comment
Add comment · Show 2
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 Herman-Tulleken · Jun 13, 2011 at 11:28 AM 0
Share

Remember, you can format code by using the button with 0s and 1s on it.

avatar image jammerjar · Jun 13, 2011 at 08:42 PM 0
Share

Ok was wondering how i did that - thanks

1 Reply

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

Answer by Eli-Davis · Jun 12, 2011 at 02:27 AM

for seeing if the object is getting hit 5 times I'd think you'd simple do this...

 var hit = 0;
 
 function OnCollisionEnter(){
     hit +=1;
     checkhit();
 }
 
 function checkhit(){
     if(hit == 5){
         Destroy(gameObject);
     }
 }
Comment
Add comment · Show 2 · 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 jammerjar · Jun 13, 2011 at 09:07 AM 0
Share

Hey Eli thought I would just let you know that that worked great thanks. Applied it as a separate script ins$$anonymous$$d of making it part of my instantiate script. It didnt like having two OnCollisionEnters and I couldnt make it work inside the existing function. Not sure if that's a bad thing to do but it work. Thanks

avatar image Chris D · Jun 13, 2011 at 03:47 PM 0
Share

@jammerjar if Eli's solution fixed your problem, please accept it as the answer to your question.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Scoring (i'm stuck) 3 Answers

Respawn when a character hits an object with a tag of "Kill" , but after 3 deaths the controller is destroyed 0 Answers

Help with collisions and destroy please 1 Answer

How can I remove an object when colliding with an other object? 1 Answer

destroy touch the screen on gameobject in camera 0 Answers


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