- Home /
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.
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++; }
your code has no effect on my enemys' .... any suggestion?
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.
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?)
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.
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
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);
}
}
}