Going to be quick and concise. First of all I'm a beginner to C# since mid last year. So I may not understand complex pseudo-code explanations, sorry in advance.
The problem: I have an object in my scene that has a TriggerBattle.cs and a simple box collider with is trigger enabled. When my player has entered into trigger, the TriggerBattle.cs then fades the screen out to black and then it loads into my Battle scene. After combat, I want to be able to then set up a condition to say, "I won the battle, when you load back out to the world, that object you activated into battle is no longer there!" But.. I cannot work out how to do that.
Tried: Using DontDestroyOnLoad to retain it into the Battle Scene and disabled the object to 'hide' it, then if I won the battle, a condition I had jerry-rigged up with a simple static bool to switch to true would simply destroy the gameObject.
Except, it will find the first gameObject of the same type "TriggerBattle" and destroy it regardless if it's the one I activated to battle or not. (My main issue)
Expected result: Once I have won that battle I want that gameObject in my 'World Scene' to persist being deactivated forever. I don't want it to come back when I switch scenes, only if I create a new playthrough.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TriggerBattle : MonoBehaviour
{
[SerializeField] GameObject fadeScreenObject;
[SerializeField] GameObject cameraObject;
[SerializeField] string sceneManager;
FadeImage fadeImage;
void Awake()
{
fadeImage = FindObjectOfType<FadeImage>();
cameraObject = FindObjectOfType<Camera>().gameObject;
fadeScreenObject = FindObjectOfType<FadeImage>().gameObject;
}
IEnumerator OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<Unit>().isPlayer)
{
fadeScreenObject.gameObject.SetActive(true);
yield return StartCoroutine(fadeImage.FadeSlowly(true));
yield return new WaitForSeconds(0.5f);
cameraObject.gameObject.SetActive(false);
Destroy(cameraObject);
DontDestroyOnLoad(gameObject);
gameObject.SetActive(false);
SceneManager.LoadScene(sceneManager);
}
}
}
So you can see I've tried to retain that particular object with the DontDestroyOnLoad but all it does is carry over, and then when I return back to my scene from battle it gets duplicated.