- Home /
Why OnCollisionEnter doesn't subtract properly player health
Hello everyone. I don't understand why everytime my player collides with an enemy, an amount of 5 lives is subtracted instead of 1 only!!!
This is the code of my OnCollisionEnter attached to the player:
void OnCollisionEnter (Collision collision) {
if (collision.gameObject.CompareTag ("Enemy")) {
if (immortality == false) {
if (GameManagerScript.GetInstance ().GetLives () > 0) {
int tempCalc;
tempCalc = GameManagerScript.GetInstance ().GetLives () - 1;
GameManagerScript.GetInstance ().setLives (tempCalc);
immortality = true;
if (immortality) {
StartCoroutine (Flasher ());
Invoke ("resetInvulnerability", 2);
if (GetComponent<MeshRenderer> ().material.color == Color.white || GetComponent<MeshRenderer> ().material == null) {
GetComponent<MeshRenderer> ().material = m;
GetComponent<MeshRenderer> ().material.color = c;
}
}
} else if (GameManagerScript.GetInstance ().GetLives () == 0) {
dead = true;
}
}
}
}
There are some methods not reported here: one is Flasher(), which is supposed to "flash" the player when is hit by an enemy giving some seconds of immortality, and the others are those contained within the GameManagerScript, which handle the player stats during the flow of the game.
EDIT: I tried to manage the thing with the usage of a flag, as it is suggested in this post https://answers.unity.com/questions/738991/ontriggerenter-being-called-multiple-times-in-succ.html However, it doesn't work, and my player suffers a 5 lives subtraction ins$$anonymous$$d on 1 when an enemy hits him.
Probably the problem resides on the fact that my player has a sphere collider, but I don't really know how to manage it! :-(
When OnCollisionEnter() is called, is it being called once at a time, or 5 times simultaneously? For example, if you put in a Debug.Log() just inside
if (collision.gameObject.CompareTag ("Enemy")) {
does it show up 5 times simultaneously or just once?
Just to be sure, does it decrease the number of lives by 5, or does it decrease *to* a specific value? Just making sure nothing's being overlooked.
And finally, what do your GetLives() and SetLives() functions look like? Could the problem be in those, or even in Game$$anonymous$$anagerScript.GetInstance() ins$$anonymous$$d?
I put some Debug.Log, it seems that OnCollisionEnter is called 5 times simultaneously, stopping at row 10 (so, the Flasher() coroutine is not processed).
Yeah: it decreases the number of lives by 5, not to a specific value.
Here are the two functions contained in my Game $$anonymous$$anager, GetLives() and setLives() respectively:
public int GetLives () {
return Game$$anonymous$$anagerScript.GetInstance ().lives;
}
public int setLives(int newLives) {
Game$$anonymous$$anagerScript.GetInstance ().lives = newLives;
return Game$$anonymous$$anagerScript.GetInstance ().lives;
}
Answer by Rizzutp · May 07, 2018 at 09:39 AM
Ok, the error was that a multiple PlayerController component was attached to the player through the code!
Your answer
Follow this Question
Related Questions
how would I set up a regain health ui button 1 Answer
Lerpz Health Issue 1 Answer
How to destroy and respawn the player after loosing a life? 1 Answer
Character losing life event not firing 1 Answer
How to slowly decrease health script? 3 Answers