Search Unity

Need help making tutorial windows not load on level reset.

Discussion in 'Scripting' started by topherbwell, Jun 29, 2015.

  1. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I have several game objects, each of which are just an empty object with a trigger and the following script. All of these game objects are children of one empty object in the hierarchy for sanity sake.

    What this does is, when a player walks into the trigger, the script pauses the game and enables a canvas with a tutorial window. The window has a button which closes the window and unpauses the game.

    When the player dies, the level loads. What I am wanting is for the tutorial windows to not trigger if the player dies and the level reloads.

    Currently, the tutorial messages trigger every time the level is reloaded.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class PopUpMessage : MonoBehaviour
    7. {
    8.      public Canvas message;
    9.      Collider talkbox;
    10.      public bool paused = false;
    11.  
    12.      void Start ()
    13.      {
    14.           talkbox = GetComponent<Collider> ();
    15.           message = message.GetComponent<Canvas> ();
    16.           message.enabled = false;
    17.      }
    18.  
    19.      void OnTriggerEnter(Collider other)
    20. //player walks into trigger, message window pops up and the game pauses.
    21.      {
    22.           if (other.tag == "Player")
    23.           {
    24.                message.enabled = true;
    25.                paused = togglePause();
    26.      }
    27. }
    28.  
    29.      public void okPress ()
    30. /*player presses the OK button on the window, the window closes and the game unpauses.
    31. The collider is disabled so that the window cannot be triggered again.
    32. */
    33.      {
    34.           message.enabled = false;
    35.           paused = togglePause ();
    36.           talkbox.enabled = false;
    37.      }
    38.  
    39.      bool togglePause()
    40.      {
    41.           if(Time.timeScale == 0f)
    42.           {
    43.                Time.timeScale = 1f;
    44.                return(false);
    45.           }
    46.           else
    47.           {
    48.                Time.timeScale = 0f;
    49.                return(true);
    50.           }
    51.      }
    52. }
    53.  
    I have tried using the following, but am not familiar with the usage and I am not certain that this is the way to handle this. This is my first time trying to carry information over a level load. Can anyone help me out?
    Code (csharp):
    1.  
    2.      void Awake()
    3.      {
    4.           DontDestroyOnLoad(transform.gameObject);
    5.      }
    6.  
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    What about using PlayerPrefs to record when each message has been shown? In OnTriggerEnter, get the value stored in PlayerPrefs (if any). If it indicates that the message has been shown, don't show it again.

    This way it's persistent between play sessions, too.
     
    Kiwasi and topherbwell like this.
  3. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I looked up PlayerPrefs and that looks like the right avenue to go down. Now I just have to learn how to do use it.
     
  4. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Can anyone link a good primer that someone who has never been introduced to PlayerPrefs could use? I'm finding a lot of really outdated stuff or stuff that uses all javascript. I just want a beginner video that introduces me to what playerprefs is, how to implement it, etc.

    Thanks.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not full code
    Code (csharp):
    1.  
    2.  
    3. OnTriggerEnter(...)
    4. {
    5.     if(PlayerPrefs.GetInt("Tutorial Triggered " + transform.name) != 1) // check it's not been triggered
    6.     {
    7.         PlayerPrefs.SetInt("Tutorial Triggered " + transform.name, 1); // set it to triggered in playerprefs
    8.         // do full tutorial display
    9.     }
    10.     else
    11.     {
    12.         // do mini popup reminder display
    13.     }
    14. }
    15.  
    16.  
    or something like that :)


    edit: too much sql today :(
     
    topherbwell likes this.
  6. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Thank you as always LeftyRighty! I wasn't actually looking for code for this one, but that will certainly help me considerably. I actually had never heard of PlayerPrefs until I saw the response above. I am about to watch a few tutorials; I am assuming PlayerPrefs is not just another script/class?

    Off to youtube land I go! Thanks again!
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    tbh I've not looked into how it works... just that it does :)
     
  8. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    I'd like to eventually have things like a player log and possible multiple characters to choose from. Is playerprefs kind of the magical skeleton key to all of these things?
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    in my limited understanding... playerprefs is kinda like cookies in web coding... useful for small stuff, but if you are looking for robust data saving you need a "bigger" solution.
     
    topherbwell likes this.
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    PlayerPrefs is a static class, so you can call its methods -- e.g., PlayerPrefs.GetInt() -- directly without having to create an instance of the class. It provides a platform-independent way to save lightweight data. If you're only saving a few datapoints, such as which tutorials have been triggered and which character the player has currently chosen, PlayerPrefs works well. But it's limited to 1 MB on web players, slightly more on other platforms, so as LeftyRighty said you won't want to use it for large player logs or other data of any substantial size. If your player log is relatively small, however, PlayerPrefs might be an easy way to handle it.
     
    topherbwell likes this.
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  12. topherbwell

    topherbwell

    Joined:
    Jun 8, 2015
    Posts:
    180
    Thanks everyone. It's been a crazy week and i've had no time to work on this. Hopefully in the next few days I will get some time. Appreciate the links and explainations!