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

Need HELP!!

Discussion in 'Scripting' started by Sandesh321, May 9, 2021.

  1. Sandesh321

    Sandesh321

    Joined:
    May 2, 2021
    Posts:
    53
    Hi I'm a new coder and need help.
    I'm trying to play a ads after 3 deaths. So I have a variable and when i die I increase that variable by one so when it reaches 3 it will play ads. But the after you die it reloads level and the variable always resets to 0. Does anyone know a way around this.
    Code (CSharp):
    1.  if (collisonInfo.collider.tag == "Obstacle")
    2.         {
    3.             Debug.Log("We hit an obstacle");
    4.             movement.enabled = false;
    5.             deathNum = deathNum + 1f;
    6.            
    7.             if (deathNum >= 3f)
    8.             {
    9.                 FindObjectOfType<GameManager>().PlayerAds();
    10.                 deathNum = 0f;
    11.  
    12.             }
    13.             Debug.Log(deathNum);
    14.             FindObjectOfType<GameManager>().EndGame(); // where I reload game
    15.            // FindObjectOfType<AudioManager>().Play("PlayerDeath");
    16.         }  
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    Quick solution is to make the variable static.
    Code (CSharp):
    1. static int deathNum;