Search Unity

My LiveSystem Doesn't make Sense

Discussion in '2D' started by evan_medi, Nov 24, 2017.

  1. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    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):
    1. public void Respawn()
    2.     {
    3.         currentLives -= 1;
    4. //updating the Text in the UI
    5.         livesText.text = "Lives: x" + currentLives;
    6. // if current live bigger than 0 Respawn, or else deactivate the player
    7.         if(currentLives > 0)
    8.         {
    9.             StartCoroutine("RespawnCo");
    10.         } else
    11.         {
    12.             thePlayer.gameObject.SetActive(false);
    13.         }
    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
     
    Last edited: Nov 24, 2017
  2. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    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)
     
  3. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    that was my first guess also
    but puting the function in a Coroutin didn't help and Reswpawn() was still called twice
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe seeing the code that calls the Respawn method , as well as maybe the coroutine might shed some light on what's wrong.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 ;)
     
  6. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    okay i will
    should i post the full script or just the part related to the Respaw() function
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just curious about the part where it calls the method. I guess I'm just curious how it gets called twice.. :)
     
  8. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    Code (CSharp):
    1.  
    2. void Update () {
    3.        if(healthCount <= 0 && !respawning)
    4.         {
    5.             Respawn();
    6.             respawning = true;
    7.         }
    8.  
    9.    }
    10.   public void Respawn()
    11.     {
    12.         currentLives -= 1;
    13. //updating the Text in the UI
    14.         LivesText.text = "x" + currentLives;
    15. // if current live bigger than 0 Respawn, or else deactivate the player
    16.         if (currentLives > 0)
    17.         {
    18.             StartCoroutine("RespawnCo");
    19.         } else
    20.         {
    21.             thePlayer.gameObject.SetActive(false);
    22.         }
    23.  
    24.     }
    25.  
    26.     public IEnumerator RespawnCo()
    27.     {
    28.  
    29.         thePlayer.gameObject.SetActive(false);
    30.         Instantiate(deathExplosion, thePlayer.transform.position, thePlayer.transform.rotation);
    31.         yield return new WaitForSeconds(timeToRespawn);
    32.  
    33.         healthCount = maxHealth;
    34.         respawning = false;
    35.         UpdateHeartMeter();
    36.  
    37.         coinCount = 0;
    38.         coinText.text = "Coins: x" + coinCount;
    39.  
    40.         thePlayer.transform.position = thePlayer.respawnPosition;
    41.         thePlayer.gameObject.SetActive(true);
    42.  
    43.         for (int i = 0; i <objectsToReset.Length; i++)
    44.         {
    45.             objectsToReset[i].gameObject.SetActive(true);
    46.             objectsToReset[i].ResetObjects();
    47.         }
    48.     }
    Let me know if there some tag that you don't understand in the code
     
    Last edited: Nov 26, 2017
  9. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    Theres no call to Respawn() in the code exerpt
     
  10. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    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
     
    Last edited: Nov 26, 2017
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  12. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    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
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, sure, nothing to it.. I'd write
    Code (csharp):
    1.  
    2. print("Health Count = " + healthCount);
    3.  
    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 :)
     
  14. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    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):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.tag == "KillPlane")
    4.         {
    5.             //gameObject.SetActive(false);
    6.             //transform.position = respawnPosition;
    7.  
    8.             theLevelManager.Respawn();
    9.         }
    10. // ignore this section as its only respossible to respawn the player on a specific place when he dies
    11.         if (other.tag == "CheckPoint")
    12.         {
    13.             respawnPosition = other.transform.position;
    14.         }
    15.     }
     
    Last edited: Nov 29, 2017
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  16. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    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):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.tag == "KillPlane")
    4.         {
    5.             //gameObject.SetActive(false);
    6.             //transform.position = respawnPosition;
    7.             //theLevelManager.respawn();
    8.               theLevelManager.currentlives = 0;
    9.         }
    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
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, sorry we never figured it out. You okay with this solution for now? :)
     
  18. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    yh its working so i can't quite complain
    as long that double respawning won't mess with my script in the feature
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
     
  20. evan_medi

    evan_medi

    Joined:
    Nov 16, 2017
    Posts:
    13
    i will :)
    thnk you for your feedback btw i don't think that i've thanked you before
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool. You're welcome :)