I’m creating this 2D platformer game in Unity. In the game, there is a place where you switch scenes to go to another part of the game called the ‘Quiet Zone’. At the start of the game, I want the health to reset to 100% but use PlayerPrefs to keep the health in between scenes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class CharacterController : MonoBehaviour
{
public float speed;
public float jumpForce;
public int health = 10;
public float delayDamage;
public int currentHealth;
public LayerMask groundLayer;
public LayerMask enemyLayer;
public LayerMask questionLayer;
public Rigidbody2D rb;
public BoxCollider2D boxCollider;
public TextMeshProUGUI healthText;
public GameObject deathScreen;
public GameObject questionScreen;
public GameObject redScreen;
public float qZoneStart;
public bool inQuietZone = false;
private static bool initDone = false;
// Start is called before the first frame update
void Start()
{
health = PlayerPrefs.GetInt("health");
}
// Update is called once per frame
void Update()
{
if (delayDamage > 0)
{
delayDamage -= Time.deltaTime;
}
float horizontal = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * speed * horizontal * Time.deltaTime);
if (isOnGround())
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
{
rb.velocity = Vector2.up * jumpForce;
}
}
if (delayDamage <= 0)
{
DamagePlayer();
}
healthText.text = "Health: " + (health * 10).ToString() + "%";
if (health <= 3)
{
redScreen.SetActive(true);
}
else
{
redScreen.SetActive(false);
}
if (health <= 0)
{
deathScreen.SetActive(true);
Time.timeScale = 0f;
}
qZoneStart = 30;
if (transform.position.x >= qZoneStart)
{
inQuietZone = true;
}
else
{
inQuietZone = false;
}
if (questionCollided())
{
questionScreen.SetActive(true);
Time.timeScale = 0f;
}
PlayerPrefs.SetInt("health", health);
}
private bool isOnGround()
{
RaycastHit2D raycast = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0f, Vector2.down, 0.1f, groundLayer);
return raycast.collider != null;
}
private bool enemyCollided()
{
RaycastHit2D raycast = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, Random.Range(90f, -90f), Vector2.down, 0.1f, enemyLayer);
return raycast.collider != null;
}
private void DamagePlayer()
{
delayDamage = 1;
if (enemyCollided())
{
health -= 1;
}
}
private bool questionCollided()
{
RaycastHit2D raycast = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, Random.Range(90f, -90f), Vector2.down, 0.1f, questionLayer);
if (raycast.collider != null)
{
Destroy(raycast.collider.gameObject);
return true;
}
return false;
}
}
Here, I said in Update() that the PlayerPrefs ‘health’ was equal to health. And in Start() I said to set the health to that PlayerPrefs. That means when I switch scenes, it will keep it. But when I restart the game, it will stay like that. I’ve tried many different ways like using the Awake() function, or OnApplicationQuit() but nothing seems to work. I’m not that good at Unity and C# so please help me. (this might just be a very simple problem and I am just overreacting )