1

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.

SpicyCatGames
  • 863
  • 7
  • 25
  • 1
    I would check out [`this`](https://gamedev.stackexchange.com/questions/110958/what-is-the-proper-way-to-handle-data-between-scenes#:~:text=Unity%20has%20a%20built%20in,types%20string%20%2C%20int%20and%20float%20.) thread. The accepted answer gives a lot of good options you can take. – TEEBQNE Jun 19 '21 at 06:06
  • Have you considered `ScriptableObject` asset with a single `bool` inside that is assigned true/false based on the outcome of the last battle. Then after the scene is changed back to the world one you check the value of the bool. – Nikaas Jun 19 '21 at 08:33
  • @Nikaas how would I go about testing it with your idea? I created a scriptableobject asset that has a single public static variable of type bool called wasDefeated and assigned it as false. On my BattleManager.cs script I then call it within my EndBattle() method once I win by simply putting it as such -> "TriggerManager.wasDefeated = true" but it just simply destroys all objects that have my triggerbattle.cs component attached. I know at least now I have something I may be able to work with, but maybe a need a little more guidance, cheers for the replies thus far :) – SinistarPlays Jun 19 '21 at 10:00
  • It should not be static. If your SO class is named say LastBattleOutcome and inherits from ScriptableObject then when you want to access an asset value you create serializable field of LastBattleOutcome in a script and drag the asset in the inspector. A bit how you would do with GameObject/prefabs. There are a lot of tutorials about ScriptableObjects on the Inet. – Nikaas Jun 19 '21 at 10:53
  • Search on inet about: scriptableobject createassetmenu – Nikaas Jun 19 '21 at 11:02
  • Not sure why my post has been deactivated, I am not seeing any resemblance to my specific data storing question to the one it links me towards. I'm still having trouble wrapping my head around this, but I'm going to keep it pushing and see if I can figure it out with the scriptable objects. – SinistarPlays Jun 20 '21 at 05:13
  • @SinistarPlays what you need is persistant data storage, not just storage between scenes. The easiest way is playerprefs https://docs.unity3d.com/ScriptReference/PlayerPrefs.html I saw this question before but did not have enough reputation yet to comment. The link to "This question already has answers here:" is completely unrealated. – SpicyCatGames Jun 22 '21 at 09:11
  • Was able to sought out some more clarification and assistance in the Unity forums. I mixed some of the responses from here and there to actually get what I was struggling to accomplish done! So yeah all good now. Post link for any future people out there! https://forum.unity.com/threads/how-to-destroy-a-specific-gameobject-in-a-list-based-on-a-condition.1128896/ – SinistarPlays Jun 24 '21 at 01:32

0 Answers0