Integer value randomly multiples itself by 3 or 2 for seemingly no reason.

I’m taking a course on Udemy called “Learn To Code By Making a 2D Platformer in Unity.” I got to the point where he’s teaching how create a lives system. For whatever reason, whatever integer value I put for how many lives should be lost, it gets multiplied by 3, except when it randomly gets multiplied by 2. This goes away when I move the code from the Respawn function to the Update function. I have looked all over, and have not seen anyone else encounter this problem. What is happening?

What’s probably happening is that you are running Respawn multiple times at once. I cannot be sure because I cannot see the script, but the fact that the lives lost is random points toward this being true. A good way to avoid this and give the player a little bit of an edge is to give the player temporary immunity. Try creating a Boolean called invincible and a float called timer. Then, use this:

if (invincible) {
  if (timer >= *number of seconds invincible*) {
    timer = 0;
    invincible = false;
  } else {
    timer += Time.deltaTime;

Then, you add an if statement around the code that damages the player.

Hope this helps!

Is it the one by James Doyle where they use Co-routines to delay the player respawn? My guess is that you are inadvertently repeating the Co-routine call in Update() and it’s being frame rate dependent is why sometimes It’s 3 and sometimes it’s 2. Check to be sure you aren’t calling the Respawn Co-Routine more than one time by using a public boolean to make sure it has or has not fired as well as the one that states whether or not the player has died, don’t let it get called if it has not.