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 /
avatar image
0
Question by Brijac · Jan 12, 2021 at 07:20 PM · playerprefslevelsunlockstars

How to make PlayerPrefs delete if the player didn't touch game finish collider

Hello, I'm making this game with 9 levels and in the beginning only level 1 is unlocked. Every level has three stars that can be unlocked with collecting points, something like this:

alt text


Now, I have a level finish collider at the end of each level and I would like that the next level unlocks when the player reaches that finish collider no matter how much points he collected.

As for the points and the stars, I would like to make that with playerprefs but I want playerprefs to only save if the player reaches the finish collider. If the player doesn't reach the finish collider, I want playerprefs deleted. I wrote few scripts to achieve this but the stars are unlocking even when the player dies or if I quit "play" through the editor.

This is an example of the game: alt text

So, I have three scripts, one for game finish, GameManager script and the one for playerprefs. Unfortunately, with everything that I wrote the game is saving points and adding stars even when I don't reach game finish collider. For example, if I pick three points and then stop editor play or just hit gameover collider the game still saves picked up points and adds stars. How can I make that happen ONLY if the player reaches the game finish collider?

Script for game finish:

  public class GameFinish: MonoBehaviour
  {
      void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag == "GameFinish")
          {
  
              GameManager.instance.GameIsFinished();
  
          }
      }
  
  }

This is in GameManager script:

 public bool gameFinished= false;    
  
  public void GameIsFinished()
      {
          gameisfinishedPanel.SetActive(true);
          gameFinished= true;
      }

Script with playerprefs:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     using UnityEngine.SceneManagement;
 
 public class CheckLevelsStarts : MonoBehaviour
 {
     public Button[] levelLoaders;
     public Image[] starsOne;
     public Image[] starsTwo;
     public Image[] starsThree;
     public Image[] starsFour;
 
     private void Start()
     {
         PlayerPrefs.SetInt("Scene1stars", 3);
 
         for (int i = 0; i < levelLoaders.Length; i++)
         {
             if(i == 0)
             {
                 if(PlayerPrefs.GetInt("Scene1stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
                 for (int j = 0; j < starsOne.Length; j++)
                 {
                     if (PlayerPrefs.GetInt("Scene2stars") > j)
                     {
                         starsOne[j].gameObject.SetActive(true);
                     }
                 }
             }
             if (i == 1)
             {
                 if (PlayerPrefs.GetInt("Scene2stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
                 for (int j = 0; j < starsTwo.Length; j++)
                 {
                     if (PlayerPrefs.GetInt("Scene3stars") > j)
                     {
                         starsTwo[j].gameObject.SetActive(true);
                     }
                 }
             }
             if (i == 2)
             {
                 if (PlayerPrefs.GetInt("Scene3stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                     for (int j = 0; j < starsFour.Length; j++)
                     {
                         if (PlayerPrefs.GetInt("Scene4stars") > j)
                         {
                             starsThree[j].gameObject.SetActive(true);
                         }
                     }
                 }
             }
             if (i == 3)
             {
                 if (PlayerPrefs.GetInt("Scene4stars") == 0)
                 {
                     levelLoaders[i].interactable = false;
                 }
             }
         }
     }
 
     public void LoadScene1()
     {
         SceneManager.LoadScene(1);
     }
     public void LoadScene2()
     {
         SceneManager.LoadScene(2);
     }
     public void LoadScene3()
     {
         SceneManager.LoadScene(3);
     }
     public void LoadScene4()
     {
         SceneManager.LoadScene(4);
     }
 }
 


game.png (10.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Jan 12, 2021 at 09:52 PM

You should make a level class that has all the attributes you want and then have an array of that class, would be a lot more readable. So you say you only want to save the playerPrefs if the player reaches the finish, the player reaches the finish when they hit a collider, so why not just call the playerprefs save from the collision method, then you don't have to worry about deleting prefs if you don't finish. So instead of Calling SetInt in start you call it in your onTrigger.

Comment
Add comment · Show 2 · 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 Brijac · Jan 13, 2021 at 04:46 PM 0
Share

@logicandchaos

I'm not a very good programmer so I don't know if you mean making a script for the game finish that I will attach to a game finish collider, something like this:

 public class GameFinish: $$anonymous$$onoBehaviour
 {
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "GameFinish")
         {
 
             Game$$anonymous$$anager.instance.GameIsFinished();
 
         }
     }
 
 }

Then, add something like this in my Game$$anonymous$$anager script:

 public bool gameFinished= false;    
 
 public void GameIsFinished()
     {
         gameisfinishedPanel.SetActive(true);
         gameFinished= true;
     }

And then in my script "CheckLevelsStarts"(the one with playerprefs) add something like "if (gameFinished)" and then under that all the playerprefs code? I'm not really sure how I would write that.

avatar image Brijac · Jan 14, 2021 at 06:39 PM 0
Share

Unfortunately what I wrote yesterday is not working.

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

116 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 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 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 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 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 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

Why does my level progress reset after clicking the level menu button but doesnt reset after clicking the lvel menu button from the win screen? 1 Answer

Dynamic Star System with PlayerPrefs 1 Answer

PlayerPrefs not save my unlock button (Levels lock/unlock) 0 Answers

PlayerPrefs Problem crashing with SetBool 2 Answers

Purchase Level Unlock System 1 Answer


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