I have a player life script that manages the death and reset methods-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
public class PlayerLife : MonoBehaviour
{
[SerializeField] private AudioSource playerDeath;
private Rigidbody2D rb;
private Animator anim;
private Vector2 currentPos;
private GameObject player;
private GameObject cherry;
private SpriteRenderer spriteRenderer;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
currentPos = this.transform.position;
}
private void Update()
{
player = GameObject.FindWithTag("Player");
cherry = GameObject.FindWithTag("Cherry");
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
Die();
}
}
private void Die()
{
playerDeath.Play();
rb.bodyType = RigidbodyType2D.Static;
anim.SetTrigger("death");
HealthSystem.health--;
if (HealthSystem.health <= 0)
{
Invoke(nameof(EndGame), 1.0f);
}
else
{
}
StartCoroutine(DelayedFunction());
}
private void EndGame()
{
SceneManager.LoadScene("End_Screen");
}
IEnumerator DelayedFunction()
{
yield return new WaitForSeconds(1.0f);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
And I have a script for item collection-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using static UnityEditor.Progress;
public class ItemCollector : MonoBehaviour
{
private int cherries = 0;
[SerializeField] private TMPro.TMP_Text cherriesText;
[SerializeField] private AudioSource collectSoundEffect;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Cherry"))
{
collectSoundEffect.Play();
Destroy(collision.gameObject);
cherries++;
cherriesText.text = "Cherries : " + cherries;
Debug.Log("Cherries: " + cherries);
}
}
}
The issue I am having is that when my player dies and the scene resets(Load scene)
All the collectible game objects reset as well.
How can I make specific game objects not reset upon load scene?
I thought about making the player reset to a position rather than loading the scene, but that causes issues with the death animation. (the sprite is disabled after the death animation).
Would appreciate any ideas.