so guys as the title says im currently working on a livesystem for my platformer but something is off and i don't know why to explain my code the idea is very simple every time the player respawn i take one Life from him and when he hit 0(or bellow) the player get desactivated in order to display the game menu in the future Code (CSharp): public void Respawn() { currentLives -= 1; //updating the Text in the UI livesText.text = "Lives: x" + currentLives; // if current live bigger than 0 Respawn, or else deactivate the player if(currentLives > 0) { StartCoroutine("RespawnCo"); } else { thePlayer.gameObject.SetActive(false); } after testing the code is working just fine but for some reason it every time the player die/respawn it takes 2 Lives instead of one, which doesn't make sense atleast for me i've ran some debugging and it seem like this fuction CurrentLives -= 1; ran for two times in row and thats what causing the problem as the picture bellow shows but i have no idea of why such a thing happens
Guessing that Respawn() is called twice, possibly over 2 frames, and possibly due to a delay from handling the actual respawning logic in a coroutine (I suspect you want to handle that in the same frame/update that the player actually dies, or at least ensure that when animating a death you don't trigger it multiple times)
that was my first guess also but puting the function in a Coroutin didn't help and Reswpawn() was still called twice
Maybe seeing the code that calls the Respawn method , as well as maybe the coroutine might shed some light on what's wrong.
If you want, please post (with code tags) the script here Most people, including myself, do not really want to click links, or worse .. download files just to read your code It's much easier here in the forums
Just curious about the part where it calls the method. I guess I'm just curious how it gets called twice..
Code (CSharp): void Update () { if(healthCount <= 0 && !respawning) { Respawn(); respawning = true; } } public void Respawn() { currentLives -= 1; //updating the Text in the UI LivesText.text = "x" + currentLives; // if current live bigger than 0 Respawn, or else deactivate the player if (currentLives > 0) { StartCoroutine("RespawnCo"); } else { thePlayer.gameObject.SetActive(false); } } public IEnumerator RespawnCo() { thePlayer.gameObject.SetActive(false); Instantiate(deathExplosion, thePlayer.transform.position, thePlayer.transform.rotation); yield return new WaitForSeconds(timeToRespawn); healthCount = maxHealth; respawning = false; UpdateHeartMeter(); coinCount = 0; coinText.text = "Coins: x" + coinCount; thePlayer.transform.position = thePlayer.respawnPosition; thePlayer.gameObject.SetActive(true); for (int i = 0; i <objectsToReset.Length; i++) { objectsToReset[i].gameObject.SetActive(true); objectsToReset[i].ResetObjects(); } } Let me know if there some tag that you don't understand in the code
i Updated the code hope it make sense now BTW now i noticed that its being random everytime i run the game sometimes take one life sometimes take two
Nothing really looks wrong. I'd just double check with some Debug Logs at a few places to see when it changes the first and second time. Then track down the issue from there. Maybe something will stand out... On a side note, that shouldn't be related to your issue exactly, but something I like to point out to people with code like this is: If you have a section of code that takes damage for your player, you should check if their health <= 0 in that section, and skip the Update check. As I said, it's just "useful" in general, and not really the cause here.
the problem is that im new to coding so im having a hard time debugin the script for syntax errors, i'd appreciate if you can walk me through it
Well, sure, nothing to it.. I'd write Code (csharp): print("Health Count = " + healthCount); Just put that in a few spots. Like before you call respawn , inside respawn, in the coroutine. Maybe add the name of the method inside the print statement. One thing that I wonder if is maybe the case, is there any chance you have duplicates of this script in the scene? If not, I would just try these print statements to see if you can narrow your search
i've wrote the Print command at the void start and also before i call respawn(); and after and this is the results in the console at least that helped us confirming that the respawn(); script is runing twice for some reason i think i've made some advancement remember when i told you that its pretty random and sometimes takes one life and sometimes takes two(as the image in top) in short when the player fall out the platform i've set a box Collider 2d that is being triggered to run respawn() and when the player hearts reach zero by stomping on enemies it runs a Hurtplayer Script which also call the respawn() function so my guess would be that there is something wrong with the kill plane collider since the propblem only occur when the player falls Code (CSharp): void OnTriggerEnter2D(Collider2D other) { if (other.tag == "KillPlane") { //gameObject.SetActive(false); //transform.position = respawnPosition; theLevelManager.Respawn(); } // ignore this section as its only respossible to respawn the player on a specific place when he dies if (other.tag == "CheckPoint") { respawnPosition = other.transform.position; } }
Okay, so you made a little progress and that's good. It looks like in the second screen shot, that you don't lose a life right away with respawn, but you do on the 2nd print statement? You can try to add the game object name to the print when you lose a life, maybe that'll show you 2 things are making you lose a life sometimes.. stuff like that. hope that helps and you can narrow it down further.
after further investigation i found a work arround the issue which was instead of calling the respawn function when the player hit KillPlane now it only update the Heartmeater to 0 and let the update void in the levelmanager takeoff the rest Code (CSharp): void OnTriggerEnter2D(Collider2D other) { if (other.tag == "KillPlane") { //gameObject.SetActive(false); //transform.position = respawnPosition; //theLevelManager.respawn(); theLevelManager.currentlives = 0; } the probleme is still there and the respawn fuction is still being called twice but at least now the live system is working properly it still bugs my mind why it is being called twice guess will never know hehe
yh its working so i can't quite complain as long that double respawning won't mess with my script in the feature
Okay. Well, if it's not interfering for now, I guess it's okay to go forward. Keep working on your game, and perhaps later you will solve it