Play scene only on initial load
I've got a shooter Android app that, on first opening, I want to play a prologue to set up the story of the game. However, I don't want it to play every time the user opens up the app as it will get annoying. Is there any way of doing this?
I was thinking something along the lines of this (pseudo code)
Press play button in main menu > Application.LoadLevel('Prologue') > Play prologue then automatically start game > Application.LoadLevel('Level1').
Next time opening: Press play button > Has prologue already been played? If yes, Application.LoadLevel('Level1')
Answer by HarshadK · Feb 23, 2015 at 02:26 PM
Yes, you are right.
You can use PlayerPrefs to set if the user has already viewed prologue or not.
The code can be something like:
void Start()
{
// If there is no entry for isFirstTime means it is first time or if there is entry and it is not one means it is first time
if(!PlayerPrefs.HasKey("isFirstTime") || PlayerPrefs.GetInt("isFirstTime") != 1)
{
// Show your prologue here.
// Now set the value of isFirstTime to be false in the PlayerPrefs.
PlayerPrefs.SetInt("isFirstTime", 1);
PlayerPrefs.Save();
}
}
Answer by TheeDuran · Sep 12, 2015 at 05:55 PM
I don't know if you Figured it out yet but this is how i did it and it worked i use this for my score also.
void Start()
{
if ( PlayerPrefs.GetInt ("isFirstTime") != 1)
{
//Prologue that you want to use
PlayerPrefs.SetInt("isFirstTime",1);
}
}
Worked for me But i don't Know if it is bad code since iam still a beginner. @HarshadK
Answer by hollym16 · Feb 23, 2015 at 03:45 PM
I've constructed this script based on what HarshadK said and other bits I've found on the internet and it works for me:
private var levelReached : int;
function Start(){
//Allows time for text to be read
yield new WaitForSeconds (5.00);
//Checks PlayerPrefs number
levelReached = PlayerPrefs.GetInt("SavedLevel");
//If playerPrefs is 0 or above, goes from main menu to first game level
if (levelReached >= 0 ){
Application.LoadLevel(2);
}
else{ // moves to prologue
Application.LoadLevel(1);
}
}
//This resets the playerprefs in order to test in Editor
function Update(){
if (Input.GetKeyDown (KeyCode.Return)) {
PlayerPrefs.SetInt("SavedLevel",0); Application.LoadLevel(1); } }
EDIT: This doesn't actually work properly so I tried to tweak it:
function Start(){
//Allows time for text to be read
yield new WaitForSeconds (5.00);
if(!PlayerPrefs.HasKey("SavedLevel")||PlayerPrefs.GetInt("SavedLevel")!=1){
Application.LoadLevel (1);
PlayerPrefs.SetInt("SavedLevel", 1);
PlayerPrefs.Save();
}else{
Application.LoadLevel (2);
PlayerPrefs.SetInt("SavedLevel", 2);
PlayerPrefs.Save();
}
}
This nearly works but I need some help. On startup it goes from scene one, to prologue to game. Then when the player dies it loops back to scene one then goes to game. This is all great, but when it loops back to scene one, it then goes back to the prologue when its meant to go to the game (as the prologue has already played).
Wow, that was complicated! Can anyone help? I'm not sure what I've done wrong
Your answer
Follow this Question
Related Questions
Destroyed object in DontDestroyOnLoad does not recreate in its main scene 0 Answers
Main Menu Help 2 Answers
Game working in pc but not in Android While Testing 0 Answers
Unity 5 Player in air while in play mode 0 Answers
How can you make a level fly in and fall out as the player progresses? 0 Answers