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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to reset coins when restart scene

Discussion in 'Scripting' started by Styro8, Mar 17, 2020.

  1. Styro8

    Styro8

    Joined:
    Mar 17, 2020
    Posts:
    16
    This is my first game and i have some issues, i added coins recently and all works fine but when i die as a player and level resets (scene resets) they don't resets, when level is completed it also don't reset but i want only to reset when you die, example: If you have 6 coins and finish level 1, at level 2 you have 6 coins, if you pick 2 more coins and have 8 coins at level 2, when you die to reset coins to 6, not to stay 8.
    Somebody help please. Thanks!
    Here are my Manager, Coins and End-Trigger codes:
    https://gist.github.com/Styro8/825ddf0f074d4e701fb67841c493033a
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Best way to reset a screen fully is to reload it. Hands down this will cause you the least problems all around.

    If you don't want to reload part of your scene then you can break it into two separate scenes, and have the permanent part "guest load" the second part, using Additive scene loading. This makes both scenes effectively active at the same time. When it is time to reset, you would unload the previous scene and reload a new one.
     
  3. Styro8

    Styro8

    Joined:
    Mar 17, 2020
    Posts:
    16
    I am new here, so i don't really understand
     
  4. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    Your moneyAmount is static variable, which doesn't reset when you reload the scene. If you want to use it you probably need a separate none static variable for the money earned in current scene. If the scene/level is completed correctly, then you can add it to the static variable.
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This ^^^

    When a scene is reloaded, any instances of objects in your scene are also reloaded, but setting a variable static is a purposeful way of making the variable independent of any instance of that class. If you want all your variables to be reset on scene load, don't use static variables, or manually reset them on scene load.

    Silly example of resetting a static variable:
    Code (csharp):
    1. public static int MyStaticInt;
    2.  
    3. void Start()
    4. {
    5.     MyStaticInt = 0;  //This class only runs Start on scene load, so I reset it here
    6. }
    Note that static variables are a common way of passing information between scenes for this reason.
     
    Kurt-Dekker likes this.
  6. Styro8

    Styro8

    Joined:
    Mar 17, 2020
    Posts:
    16
    Yes but i don't want to reset it, i want, if you for example finish level 1 with 4 coins, level 2 will every time when you die reset with money amount you had when you finished level 1, so if i type


    1. public static int MyStaticInt;

    2. void Start()
    3. {
    4. MyStaticInt = 4; //This class only runs Start on scene load, so I reset it here
    5. }
    That won't work perfectly, cause you can finish level 1 with 3 coins, not 4.
     
  7. Styro8

    Styro8

    Joined:
    Mar 17, 2020
    Posts:
    16
    I made it, working now. Thanks for helping!