How to make GameObjects not reset on scene load

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.

To prevent specific GameObjects from being reset upon scene load, you can use the DontDestroyOnLoad method. However, keep in mind that using DontDestroyOnLoad could lead to multiple instances of the same object if you’re not careful. You can implement a Singleton pattern to avoid this issue.

First, create a script for the collectible objects that you want to persist between scene loads:

using UnityEngine;

public class Collectible : MonoBehaviour
{
    private static Collectible instance;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

Attach this script to the collectible objects you want to persist between scene loads. When the scene is reloaded, the objects with this script attached will not be destroyed.

To ensure that collectibles are not duplicated when reloading the scene, you can modify the ItemCollector script. Instead of destroying the collected object, you can deactivate it:

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Cherry"))
    {
        collectSoundEffect.Play();
        collision.gameObject.SetActive(false);
        cherries++;
        cherriesText.text = "Cherries : " + cherries;
        Debug.Log("Cherries: " + cherries);
    }
}

By deactivating the object instead of destroying it, the object will remain in the scene hierarchy and will not be duplicated when the scene is reloaded.