Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by hollym16 · Feb 23, 2015 at 02:22 PM · androidplayerscenescriptingbasicslevel

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')

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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();
      }
  
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges