- Home /
Don't destroy on collision
Hi, I had a question about this little script I made. It goes on an instantiated bullet. Now, my problem is, it always destroys itself, while I actually want it to survive whhen in contact with the player. What am I doing wrong here?
function OnTriggerEnter (other : Collider) {
if(other.CompareTag ("OtherPlayer")){
other.GetComponent(PlayerVariableHandler).hp -= Damage;
if(DestroyOnImpact){
Destroy(gameObject);
}
}
if(other.CompareTag ("Player")){
}
else{
if(DestroyOnImpact){
Destroy(gameObject);
}
}
}
Well, I'm not trying to... You see it says Destroy, when the tag is Other Player or else... But when it's 'Player' it doesn't call the Destroy function. So how can I distinguish my Player hit an my Else hit?
yes for bullits you should use a raycast as you can miss the player because it passes the player inbetween frames using a raycast will avoid that from happening. your else statement is killing it thou you have set up to destroy it no matter what collision it makes.
Answer by Lttldude · Apr 18, 2012 at 04:02 PM
Made Some changes, give this a try:
function OnTriggerEnter (other : Collider) {
if(other.CompareTag ("OtherPlayer")) {
other.GetComponent(PlayerVariableHandler).hp -= Damage;
if(DestroyOnImpact){
print("Destroyed1 by hitting: " + other.gameObject.name); //this way you can tell which conditional statement is destroying it
Destroy(gameObject);
}
}
//I made this an else if because if it hi a OtherPlayer, then this will always be true
else if(!other.CompareTag ("Player")){ //instead of having a blank conditional statement to do, just made it true if its NOT the player tag
if(DestroyOnImpact){
print("Destroyed2 by hitting: " + other.gameObject.name);
Destroy(gameObject);
}
}
}
Maybe it is never hitting the player and it's colliding with something else beforehand, this should tell you when it's being destroyed and from what collider.
Ha thanks for reformulating... Thanks to your print Script I now know what was going wrong. You see the bullets hit each other when my character moves forward. Thanks alot!
Your answer
![](https://koobas.hobune.stream/wayback/20220613070457im_/https://answers.unity.com/themes/thub/images/avi.jpg)