Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Devlus · Jan 28, 2011 at 03:01 PM · collisiondamagefor-loophealth

For loop on collision (two hit kill)

ok so i thought this would have been a super easy script to write, but it seems i was wrong, im trying to have an ammo collide with an object twice and on the second hit destroy it. (a 2 hit kill) I am trying to use a for loop todo this but if you know another way it would be great!

 function OnCollisionEnter(theCollision : Collision){
        if(theCollision.gameObject.name == "bull(Clone)"){
          var i : int = 0;
            for(i = 0;i>= 2;i++)
            if (i == 2) 
            {
            Destroy(gameObject);
            }

oooo, and i'm not concerned with the brackets, this isn't my EOF, just a snippet.

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
0
Best Answer

Answer by Jessy · Jan 28, 2011 at 03:37 PM

You need to track your variables correctly. You don't need a loop at all. Your loop would never run anyway; I'd recommend further study into loops.

var hitsToKill : int;

private var hits : int;

function OnCollisionEnter (theCollision : Collision) { if (theCollision.gameObject.name == "bull(Clone)") if (hits == hitsToKill - 1) Destroy(gameObject); else hits++; }

Comment
Add comment · Show 6 · 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 Devlus · Jan 28, 2011 at 03:56 PM 0
Share

your code has no effect on my enemys' .... any suggestion?

avatar image Jessy · Jan 28, 2011 at 04:25 PM 0
Share

Fix your logic. I provided code that solves the case you presented. You have not provided much info about your scene, so no one can help further as it is. Get everything working piece by piece.

avatar image Devlus · Jan 28, 2011 at 10:50 PM 0
Share

Jessy, if "hits" is a integer variable doesn't it need to have an initial value... could that be the problem? (or is the default 0?)

avatar image Jessy · Jan 28, 2011 at 11:08 PM 0
Share

The default is zero. Have you set up hitsTo$$anonymous$$ill? I'm sorry that I forgot to put : int before, but you shouldn't just be copying and pasting $$anonymous$$dlessly. You need to understand the code. If you don't, just ask.

avatar image Devlus · Jan 28, 2011 at 11:19 PM 0
Share

ya in all honesty im not very familiar with private variables but I understand their function, just not when it is appropriate to use them

Show more comments
avatar image
0

Answer by Mai hime · Jan 28, 2011 at 05:18 PM

you can do this:

 var hits :int=0;
function OnCollisionEnter (theCollision :Collision){
if(theCollision.gameObject.name == "bull(Clone)"){
   hits++;
   if(hits==2){Destroy(gameObject);
           }
         }
    }

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 Jessy · Jan 28, 2011 at 11:04 PM 0
Share

Yes, I posted that code already. (Close enough.)

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

No one has followed this question yet.

Related Questions

How to cause damage on collision? 1 Answer

How i make the player damage the enemy specific enemy he is colliding with 1 Answer

Health system final 1 Answer

This collision Enemy health code suddenly stopped working. 1 Answer

Script for enemy health loss on contact with bullet prefab 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