To save the game data about how many doors the players have entered, I use PlayerPrefs.
PlayerPrefs help our project to track players activities. It could respond to specific conditions that players have met.
In the project, playerPrefs is used to count the amount of the doors( they are designed as paintings in the main scene) that players had entered. If players entered the doors for 3 times, the exit would be spawned out in the game scene.
Here are some scripts of how data saving works in the project.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActiveExit : MonoBehaviour
{
public GameObject table;
public GameObject UIHint;
private int doorEnteredAmount;
// Start is called before the first frame update
void Start()
{
table.SetActive(false);
UIHint.SetActive(false);
doorEnteredAmount = PlayerPrefs.GetInt("DoorEntered");
Debug.Log("now has entered" + doorEnteredAmount + "doors");
}
// Update is called once per frame
void Update()
{
if(doorEnteredAmount >= 3)
{
table.SetActive(true);
UIHint.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorAmountCount : MonoBehaviour
{
private int doorAmount = 0;
// Start is called before the first frame update
void Start()
{
doorAmount = PlayerPrefs.GetInt("DoorEntered");
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "MainCamera")
{
doorAmount++;
PlayerPrefs.SetInt("DoorEntered", doorAmount);
Debug.Log("door entered" + doorAmount);
}
}
}