Affecting Game Objects in another Scene

Hello there,

I’m currently working on a project that has an overworld in which the player spends most of their time, but when they collide with a wandering enemy, they get loaded into a different scene where they resolve the fight using turn-based combat. The problem I’m having is that when the combat finishes, the enemy’s overworld GameObject should be destroyed so that the player cannot collide with it again.

The code I have to load a player into the battle scene is this:

public class EnemyWorldBehavior : MonoBehaviour
{
    public string battleScene;
    
    void OnTriggerEnter(Collider collider)
    {
        if(collider.gameObject.tag == "Player" && battleScene != null)
        {
            SceneManager.LoadScene(battleScene, LoadSceneMode.Single);
            // This is where I'd destroy the Enemy GameObject
        }
    }

    //TODO: Add AI that makes the enemy wander throughout the scene somewhat
}

And the code I have that ends the battle is this:
(Note: I followed Brackey’s Tutorial on turn-based combat, so the rest of the code is almost a carbon copy of that. Some changes were made to better suit my project and fix a glaring issue in his tutorial)

IEnumerator EndBattle()
    {
        if(state == BattleState.WIN)
        {
            dialogueText.text = "That's a win for you!";
            yield return new WaitForSeconds(2);
            SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
        }
        else if(state == BattleState.LOSE)
        {
            dialogueText.text = "You have died...";
        }
    }

I have it set up so that every enemy prefab can go to their own battle scene (which is my current workaround for making each battle pseudorandom with different numbers of enemies appearing in each battle). How can I set it up so that once the battle is resolved (and the player wins) the enemy GameObject in the overworld either calls a function to drop loot and die or is just simply destroyed?

You need to have some code that manages the scene switching, that does not itself get swapped along with the scene change. A GameManager.

This is an attempt at how this can be done. But it’s not tested and not finished, but you can get an idea.

public class GameManager : MonoBehaviour
{
    //implemented as singleton do not destroy

    int state = 0;
    void Update()
    {
        switch(state)
        {
            case 0:
                SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
                state++;
                break;
            case 1: //in overworld
                if(EnemyWorldBehavior.theOverWorld.enterBattle)
                {
                    SceneManager.LoadScene("BattleScene", LoadSceneMode.Single);
                    state++;
                }
                break;
            case 2: //in battle scene
                if(BattleScene.theBattle.gameOver)
                {
                    SceneManager.LoadScene("SampleScene", LoadSceneMode.Single);
                    EnemyWorldBehavior.theOverWorld.Init();
                    state = 1;
                }
                break;
        }
    }
}

public class EnemyWorldBehavior : MonoBehaviour
{
    internal static EnemyWorldBehavior theOverWorld = null;
    internal bool enterBattle = false;
    
    private void Awake()
    {
        theOverWorld = this;
    }
    public void Init(/*how to init*/)
    {
        //delete the enemy here
        //...
    }
    void OnTriggerEnter(Collider collider)
    {
        if(collider.gameObject.tag == "Player")
        {
            enterBattle = true;
        }
    }
}
public class BattleScene : MonoBehaviour
{
    internal static BattleScene theBattle = null;
    internal bool gameOver = false;

    private void Awake()
    {
        theBattle = this;
    }
    void Update()
    {
        //set gameOver=true when win or lose
        //the scene will change when set
        //...
    }
}

after being put into the overworld again and winning write some code that destroys the enemy