- Home /
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?
Variables don't "randomly" multiply themselves, especially not if the observed behaviour changes when you move the code. Post the class that has Respawn()
function in it, otherwise there is no telling what affects the number of lives lost.
Answer by DawdleDev · May 02, 2018 at 03:30 PM
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!
Answer by Pinkuboxu · May 02, 2018 at 08:12 PM
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.
Your answer
Follow this Question
Related Questions
bool doesn't change?? 0 Answers
Hello, could I get some help with my inv system? 1 Answer
Make objects move around, then bounce off and continue moving. NOT WORKING 1 Answer
How to use a timer with a button 2 Answers
How to use quaternions to change the rotation of a object when you have a joint linking 2 objects? 0 Answers