Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 24, 2016 at 04:05 PM by ethranes for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ethranes · Jul 24, 2016 at 08:44 AM · c#levelscene-switchingscene-changelevel load

Next Level to unlock when first level is completed

Hi,

I have a problem at the moment where I have a level unlock system that should unlock when a certain scene is closed. I got the code from a youtube video so I'm not sure where I need to edit it, there are 2 scripts that make up the system a level manager which governs the system overall, and a gamemanager which is applied to the scene which should unlock the next level.

The main issue I have is that the levels need to be called specific things, so in the code if you finish Level1 it unlocks level2. Also the first level unlocks by looking for level1, so normally loading and finishing level1 would unlock level 2. My difficulty is that I have multiple scenes that need to be completed for level2 to be unlocked. So I have level1, level1.2 and level1.3, the code unlocks level1 at start but will also unlock level2 as it checks that level1 has loaded to load level2, and I want level2 to start when I finish level1.3 and not level1.

Here are the relevant parts of the code for each script;


  • levelmanager;


       [System.Serializable]
         public class Level
         {
             public string LevelText;
             public int UnLocked;
             public bool IsInteractable;
         }
         public GameObject levelButton;
         public Transform Spacer;
         public List<Level> LevelList;
     
         void Start () 
         {
             FillList ();
         }
         
         void FillList()
         {
             foreach (var level in LevelList) 
             {
                 GameObject newbutton = Instantiate (levelButton) as GameObject;
                 LevelButtonNew button = newbutton.GetComponent<LevelButtonNew> ();
                 button.LevelText.text = level.LevelText;
     
                 if (PlayerPrefs.GetInt ("Level" + button.LevelText.text) == 1) //this if statement unlocked the first level when a player opens the game for the first time
                 {
                     level.UnLocked = 1;
                     level.IsInteractable = true;
                 }
     
                 button.unlocked = level.UnLocked;
                 button.GetComponent<Button> ().interactable = level.IsInteractable; 
                 button.GetComponent<Button> ().onClick.AddListener (() => loadLevels ("Level" + button.LevelText.text )); //loads a level based on the name that the scene has been saved as
     
                 if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 0) 
                 {
                     button.star1.SetActive (true);
                 }    
     
                 if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 5000) 
                 {
                     button.star2.SetActive (true);
                 }    
     
                 if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 9999) 
                 {
                     button.star3.SetActive (true);
                 }
     
                 newbutton.transform.SetParent (Spacer, false);
             }
     
             SaveAll ();
     
         }
     
         void SaveAll ()
         {
             {
                 GameObject[] allbuttons = GameObject.FindGameObjectsWithTag ("LevelButton");
                 foreach (GameObject buttons in allbuttons) 
                 {
                     LevelButtonNew button = buttons.GetComponent<LevelButtonNew> ();
                     PlayerPrefs.SetInt ("Level" + button.LevelText.text, button.unlocked);
                 }
             }
         }
     
         void loadLevels(string value)
         {
             SceneManager.LoadScene (value);
         }
     }
    
    

  • gamemanager;


        public Text scoreText;
         public int score = 10;    
         private int LevelAmount = 7; //this needs to be updated if the level count changes
         private int CurrentLevel;
     
         void Start ()
         {
             CheckCurrentLevel ();
         }
     
         void CheckCurrentLevel()
         {
             for (int i = 1; i < LevelAmount; i++) 
             {
                 if (SceneManager.GetActiveScene().name == "Level" + i) 
                 {
                     CurrentLevel = i;
                     SaveMyGame ();
                 }
             }
         }
     
         void SaveMyGame()
         {
             int NextLevel = CurrentLevel + 1;
             if (NextLevel < LevelAmount) {
                 PlayerPrefs.SetInt ("Level" + NextLevel.ToString(), 1);//unlock next level
                 PlayerPrefs.SetInt ("Level" + CurrentLevel.ToString () + "_score", score);
             } 
             else 
             {
                 PlayerPrefs.SetInt ("Level" + CurrentLevel.ToString () + "_score", score);
             }
         }
     }
    
    
    

There may be some stray syntax because I cut code out so don't worry about that. I'm really at a loss with how to do this, I understand it's a lot to go through so any help or advice will be appreciated.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by ethranes · Jul 25, 2016 at 06:41 AM

The issue was with the gamemanager like Nitin22 mentions which is in each scene, it's not a clean way that I fixed it but it works, the gamemanager is renamed level1manager, level2manager and so on, and the following value;

          PlayerPrefs.SetInt ("Level" + NextLevel.ToString(), 1);//unlock next level

Is only set to 1 on the last scene that the player needs to reach in level one, all prior levels have the value set to 0 to stop level2 from becoming unlocked.

Thanks for the help Nitin22.

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 Nitin22 · Jul 24, 2016 at 02:53 PM

@ ethranes , I think in "GameManager" when "Start" function calls you check the current active scene which is 1 ,thereafter SaveMyGame function is called and in that you check your "nextlevel" which is 2 with "levelAmount" which is 7 less than levelAmount and you unlock the next level which is 2.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Does anyone know how to look into this sort of thing? (Desciption) 3 Answers

How to make this scene changing? 1 Answer

Multiple Cars not working 1 Answer

How do I stop the scene from switching when the gameObject is destroy / 0 Answers

Distribute terrain in zones 3 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