0
\$\begingroup\$

The script I am using determines if it is the players first time hitting the play button and if it is it will load the intro scene, but if they have played before it will take them strait to the game scene. The problem is that it just takes them strait to the game scene.

public class SeenBefore : MonoBehaviour
{
    public string SceneName;

    AsyncOperation preload;

// Start is called before the first frame update
    void Start()
    {
        preload = SceneManager.LoadSceneAsync(SceneName);
        preload.allowSceneActivation = false;
    }

// Update is called once per frame
    void Update()
    {

    }

    public void before()
    {
        if (PlayerPrefs.HasKey("HasSeenIntro"))
        {
        preload.allowSceneActivation = true;
        }
        else
        {
            PlayerPrefs.SetInt("HasSeenIntro", 1);
            SceneManager.LoadScene("Intro");

        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ How are you resetting your playerprefs for testing, to clear the game's knowledge that you've already seen the intro? \$\endgroup\$ – DMGregory Aug 11 '19 at 3:13
  • \$\begingroup\$ Im not sure i jut know that it does not work. I tried doing it on a different computer and it still didnt work \$\endgroup\$ – Ethan K-B Aug 11 '19 at 3:54
  • \$\begingroup\$ @DMGregory I am not sure on how to rest my playprefs I just delete the app and export a new one. \$\endgroup\$ – Ethan K-B Aug 11 '19 at 10:52
  • \$\begingroup\$ You mentioned app - is your game for mobile? If so, and it is Android, Unity stores PlayerPrefs in SharedPreferences, which Android will not clear on upgrade (installing over an already installed app) for an app with the same package name. \$\endgroup\$ – Joe Aug 11 '19 at 12:43
  • \$\begingroup\$ Its currently on mac because I was on vacation. It is primarily a windows app. \$\endgroup\$ – Ethan K-B Aug 11 '19 at 19:20
2
\$\begingroup\$

I'm unable to reproduce this problem.

In my tests with this code, the first time I click the button I go to the "Intro" scene.

Any subsequent run of the game, I go to the scene named in the SceneName variable.

So, this is likely due to incorrect setup in your project/scenes/inspector.

Things you can try:

  1. Verify that both scenes have been added to your Build Settings.

  2. Check that you are calling the scenes by the correct names - be sure you have strict character-for-character equivalence, or look them up by ID instead. (In particular, make sure you haven't set SceneName = "Intro", else all roads lead to Rome)

  3. Add a button to your project to delete all PlayerPrefs on demand, so you can ensure you're not getting data from past runs.

  4. Ensure you have no other scripts that are also firing off their own scene loads - try setting up a new, empty project with just three scenes: a start scene, a first-time-only scene, and a second-time-and-after scene, and only this one script.

For testing convenience, here's a version of the script with a function to reset your preferences key.

I've verified this works as desired, going to firstTimeScene the first time Transition() is called, and to fallbackScene the next time the button is pressed on a subsequent return to the menu or fresh run of the game. Calling ResetPlayerPrefs correctly resets this behaviour, so it behaves as the first time again.

using UnityEngine;
using UnityEngine.SceneManagement;

public class ScenePreloader : MonoBehaviour
{
    public string firstTimePrefsKey = "HasSeenIntro";

    public string firstTimeScene = "SceneA";
    public string fallbackScene = "SceneB";

    AsyncOperation _preload;

    void Start() {
        _preload = SceneManager.LoadSceneAsync(fallbackScene);
        _preload.allowSceneActivation = false;
    }

    public void ResetPlayerPrefs() {
        PlayerPrefs.DeleteKey(firstTimePrefsKey);
        PlayerPrefs.Save();
    }

    public void Transition() {
        if (PlayerPrefs.HasKey(firstTimePrefsKey)) {
            _preload.allowSceneActivation = true;
        } else {
            PlayerPrefs.SetInt(firstTimePrefsKey, 1);
            SceneManager.LoadScene(firstTimeScene);
        }
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ So i I understand correctly on the button I have to add the transition and the reset the playerpref void to onclick right? \$\endgroup\$ – Ethan K-B Aug 11 '19 at 19:44
  • \$\begingroup\$ You have two buttons. One to trigger your scene transition. A different button will reset your player preferences as a debugging convenience. \$\endgroup\$ – DMGregory Aug 11 '19 at 19:45
  • \$\begingroup\$ Ah gotcha. This works very well and I appreciate your help. \$\endgroup\$ – Ethan K-B Aug 11 '19 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.