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][1]
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:
[174272-game.png*_|174272]
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*.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*.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*.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*.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);
}
}
*[1]: https://image.shutterstock.com/image-vector/typical-level-screen-mobile-game-600w-482017078.jpg*_
*