Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Counter goes up by 2 instead of 1

Discussion in 'Editor & General Support' started by Wallisch_pls, May 28, 2014.

  1. Wallisch_pls

    Wallisch_pls

    Joined:
    Oct 14, 2013
    Posts:
    13
    I made a simply death counter so that every time the player hits a certain object, their coins reset, transported back to spawn, and +=1 for the death variable

    script for death time (connected to object it comes in contact with)

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. function OnTriggerEnter2D(info : Collider2D)
    5.    {
    6.       if (info.tag == "Player")
    7.       {
    8.        Application.LoadLevel(Application.loadedLevelName);
    9.        coinss.score = 0;
    10.        addDeath.death += 1;
    11.       }
    12.    }
    13.  
    script for guiText

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. static var death = 0;
    5.  
    6. function Update () {
    7.     guiText.text = "Deaths: " + death;
    8. }
    9.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
    atleast in 4.3.x there used to be bug, where OnTriggerEnter2D gets called manytimes..(not just when player enters, but when moving inside trigger too)

    try adding some boolean, to check if "entered == false", then do the +=1, and also set enterted = true..

    Also you are starting to load level before the score and death values are set.. not sure if that could cause problems sometimes..
     
  3. Wallisch_pls

    Wallisch_pls

    Joined:
    Oct 14, 2013
    Posts:
    13
    so if it's a bug there is no fix?

    I tried what you said but of course it went into an infinite loop +=1, and I hate fixing those. Also where the level is actually loaded does not effect the code

    anyone else??
     
  4. Wallisch_pls

    Wallisch_pls

    Joined:
    Oct 14, 2013
    Posts:
    13
    Also inb4 obvious response:

    "make it add 0.5 then it will be 0.5 * 2, sooooo 1!!!!"

    No, that doesn't work for some reason
     
  5. Alk Studios

    Alk Studios

    Joined:
    May 28, 2014
    Posts:
    115
    Perhaps try using a boolean flag i.e.

    Code (csharp):
    1.  
    2. boolean isDead = false;
    3.  
    4. function OnTriggerEnter2D(info : Collider2D)
    5. {
    6.     if (info.tag == "Player")
    7.     {
    8.         if (!isDead)
    9.         {
    10.             coinss.score = 0;
    11.             addDeath.death += 1;
    12.             isDead = true;
    13.             Application.LoadLevel(Application.loadedLevelName);
    14.         }
    15.     }
    16. }
    17.  
    Remember to reset the boolean after respawn etc.
     
    Last edited: May 28, 2014
  6. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    If you don't have a flag and you have another collider in your Player, it will trigger the event twice. Also, like the first post said, you probably should do Application.LoadLevel after the other code.
     
  7. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  8. Wallisch_pls

    Wallisch_pls

    Joined:
    Oct 14, 2013
    Posts:
    13
    Oh no, it worked. whoops. THANKS