Reputation: 87
I want to know how to make something happen only once like a tutorial in a game which appears only when you first start your game and then when your game got saved to a further point it never appears again even when you close your game and start again.
Basically, I have seven scenes but I want to play scene 1-5 just one on the launching of the app and then next time when the user opens the app it directly jumps to the 6th scene
So I want to know how to make something happen only once in a game application.
I hope you get my point.
Upvotes: 1
Views: 404
Reputation: 148
Well, this issue can be easily addressed using PlayerPrefs. You can call PlayerPrefs.SetInt() and PlayerPrefs.GetInt(). Like so:
public class SceneRouting : MonoBehaviour {
void Awake(){
// -1 means it is not the first time the player launches the game
// while 1 means it is the first time the player launches the game
int isFirstTime = PlayerPrefs.GetInt("isFirstTime", -1);
if(isFirstTime > 0){
// Here you can load any of these scenes 1, 2, 3, 4, 5
} else {
// Here you can load scene 6
}
}
}
You can set the isFirstTime
variable anywhere in your game flow. Say, for example, when you call the Foo() method, you don't want the game to load scenes 1-5 next time it launches:
public void Foo(){
PlayerPrefs.SetInt("isFirstTime", -1);
}
Additionally, if you want your game to load scene 6 upon launch, you can set the isFirstTime
value to 1
Upvotes: 2
Reputation: 1827
You could have a look at using PlayerPrefs. There are multiple ways of using them but this could be one lightweight solution for you.
An example of how to use them would be like this for you;
private void Awake()
{
int tutorialFinishedFlag = PlayerPrefs.GetInt("TutorialFinished", 0);
if(tutorialFinishedFlag == 0)
{
ShowScenesOneToFive();
}
else
{
ShowSceneSix();
}
}
And when you've finished showing them the first 5 scenes, you can set the PlayerPref like so;
PlayerPrefs.SetInt("TutorialFinished", 1);
Upvotes: 1