Hello, so i’ve been struggling with this for a couple of Days now. I want my player to be able to go through a Level(Scene), pick up as many coins as he would like and then exit the Level(Scene), when he comes back to this Scene i want those coins he picked up not to be existent however those he didn’t pick up to still need to be there for him to pick up. I have found a way to do this but it requires me to hardcode for every coin ( See my messy Code). I’m looking for a more efficient way to do this and i would like to avoid additiveSceneLoading for performance reasons.
Thank you in advance
this is my code for picking up coins, which runs on the Coin GameObject;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class coin_pickup : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collider)
{
if (collider.CompareTag("PlayerTrigger"))
{
//Destroy(gameObject);
coinScore.coins_collected++;
if (this.gameObject.name == "C1S1")
{
CoinKillerScript.hasC1S1 = true;
Debug.Log("this is C1S1"); //---> Coin1Scene1
}
if (this.gameObject.name == "C2S1")
{
Debug.Log("this is C2S1");
CoinKillerScript.hasC2S1 = true;
}
if (this.gameObject.name == "C1S2")
{
Debug.Log("this is C1S2");
CoinKillerScript.hasC1S2 = true;
}
}
}
}
And this is my "coinKillerScript which runs on an empty DontDestroyOnLoad() Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinKillerScript : MonoBehaviour
{
public static bool hasC1S1 = false;
public static bool hasC2S1 = false;
public static bool hasC1S2 = false;
public void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void FixedUpdate()
{
if(hasC1S1 == true)
{
Destroy(GameObject.Find("C1S1"));
}
if (hasC2S1 == true )
{
Destroy(GameObject.Find("C2S1"));
}
if (hasC1S2 == true)
{
Destroy(GameObject.Find("C1S2"));
}
}
}