Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Wonder what the best practice would be for this situation

Discussion in 'Scripting' started by Sylvir, Sep 3, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    hey everyone,

    What I am wondering is what the best programming/scripting practices would be when coding in something that you only want to be called the first time a situation is met, then not again.

    for example. I want a tutorial window to show up the first time someone find 1 apple, but in the future after that event happens the first time i do not want the window to continue to keep popping up interrupting the game.

    i know that i could all ways do a simple if statement to call the window when the apple is >= 1, but what would i do to ensure this doesn't happen if the player eats the first apple, then goes and finds another?

    Thanks for any information!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Static variables are your friend! They are global, not specific to an instance of your script. So if you have this in Apple.cs:
    Code (csharp):
    1.  public static hasSeenAppleTutorial = false;
    (You may want to set it to false in some script's Awake function or in your main menu or something; static variables can persist outside of play mode sometimes)
    ....then you can check against Apple.hasSeenAppleTutorial, and set it to true when you display the tutorial.
     
    Sylvir likes this.
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    Store the value in PlayerPrefs. So when they open the game the next time, check the value in PlayerPrefs, and show the tutorial if they haven't seen it yet.

    Usually you can have a global object manager or singleton, which you can use to get and set various game values from your data store. Some people like to refactor these into several object managers, such as TutorialManager, GameManager, SoundManager etc.
     
    Sylvir likes this.
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    awesome, thank you! for some reason i did not even consider the player prefs as an option for this.. I should have.
     
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    thank you for that idea as well. Helps me to see a couple different ways you can accomplish the same goal :)
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Coroutines can also be used.
     
    Sylvir likes this.