- Home /
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:
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:
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);
}
}
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.
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.
Your answer
Follow this Question
Related Questions
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