Search Unity

Scene and Score

Discussion in 'Editor & General Support' started by Abel29, Mar 24, 2021.

  1. Abel29

    Abel29

    Joined:
    Mar 24, 2020
    Posts:
    14
    Hello everyone!
    I have a few questions about unity. I know for many of you will be stupid things but how i like to say
    no one is born learned, as a person, learns new things every day
    So, I tried to create a game that I would like to upload to android. it's just a prototype for me and my family, to see the whole process that needs to be done.
    The first scene will be with "welcome" and a start button, and then I want to put a scene with instructions and a "don't show this again" button. I think you all know what I mean. But I don't know how to do this with "don't show this again". Could you help me with a script or instructions on how I should do it?
    And the score I would like to continue from one level to another until you die. and when you return to the game to start from the level you stayed at, to have a zero score, but at the same time on the screen I want the record score you made
    So far I have done 15 levels, I made the score, but at each level it resets and I do not have a record score. After I do it will I have to put it alone on each level on the screen? or is it saved automatically at all?
    Thank you very much for your time and please excuse me if the questions were too stupid
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't worry about asking "stupid things" here. There are far worse low effort threads that appear here all the time than yours is :p

    As far as a UI window with a don't show again button or check box, see the various UI tutorials and documentation for how to actually create the UI. As far as how the don't show this again state gets preserved between runs of the game, this is a perfect use case for PlayerPrefs. When you launch the scene, you check for a specific int value stored in PlayerPrefs. If it either doesn't exist, or is set to 0, you then open the UI window. If it is set to 1 then you don't open the window. When the player moves on from that window, if they selected "don't show this again" then you save a 1 to that value, otherwise save a 0, then call playerprefs.save. There's lots of threads and probably tutorials out there on how to use PlayerPrefs, try googling them first rather than me writing you some example code here.

    As far as the score, I'm not clear if you're talking about between separate launches of the game (like a high score), or only between different levels. A high score can also be done with PlayerPrefs. A score between levels can be done a number of ways.

    If you just need to pass the score value, you can just use a static variable. This is a variable that is associated with the class itself instead of an instance of the class, so it is unaffected by scene changes.

    Code (csharp):
    1. public static int SimpleStaticIntExample = 0;
    If you need more than that, you could put your score on a script which is attached to a GameObject where you've called DontDestroyOnLoad. This will cause the GameObject to not be destroyed with all the other GameObject's in the scene when the scene gets unloaded. The next scene can just find this object using FindWithTag, a static reference to itself, or any other way you find references to components in Unity, and whatever value you had saved to the score in the previous scene will still be there in the next scene.

    Another approach is to create a separate score scene, and use additive scene loading and unloading. This is a more advanced approach, but seems to be pretty popular. It is also popular with any functionality which needs to be shared between different levels. Usually things like the UI or the controls for the game don't change between levels for example, so you might have that all in separate scenes which you don't unload between levels. You just unload the scene with the content for that specific level, and additive load the contents of the next level. This would work well for something like a score manager.