"Troubleshooting Unity 3D Turn-Based Game: Issue with Enemy Loading in Battle Scene"

Hello, how are you? I’m developing a 3D turn-based game and I’m having difficulties loading a battle scene with an enemy. The initial idea is to collide with an enemy, which should trigger the loading of a battle scene. The collision-detecting scripts for the enemy are working correctly. However, when debugging the code in the BattleManager script (which is loaded only in the second scene), the enemy variable loaded from my GameManager singleton is becoming null. Thank you for your helping!

9268830–1297557–BattleManager.cs (595 Bytes)
9268830–1297560–PlayerEnemyDetectColision.cs (927 Bytes)
9268830–1297563–GameManager.cs (397 Bytes)

When you load the second scene, the object is destroyed, unless you mark it DontDestroyOnLoad, however it is often simpler to load the scene in Additive mode.

Thank you for the response; I’m already using the function. Please find the attached screenshot.

I don’t see DontDestroyOnLoad being called or the scene loaded in Additive mode here.

My bad, wrong screenshot. I call the function in singleton

Ok my bad. You can probably debug this with a setter:

    private GameObject enemyToBringToBattle;
    public GameObject EnemyToBringToBattle
   {
       get {
           return enemyToBringToBattle;
       }
       set {
           Debug.Log($"setting {value} to enemy"};
           enemyToBringToBattle = value;
       }
   }

Hello, I apologize for the delayed response. I’ve implemented your idea, and the set and get methods are functioning correctly. However, when I attempt to access the variable in the battle scene script, it appears as null, just as shown in the screenshot

Then that means either the class that holds that variable is getting reset, or the variable that it’s trying to contain is getting deleted. You would need both(presumably) to not be destroyed on load.

as dlorre pointed out, by loading the new scene as additive(and unloading new scene when done), shouldn’t destroy anything from the first scene. If you’re doing that, and it’s still not working, it’s probably because you don’t have that singleton as a static reference, and cannot contact the correct gameManager(from old scene) for that variable in the new scene.

Thank you for the help. Indeed, loading the scene additively does solve my problem. I just need to look into this functionality a bit more because I’m not sure how costly it is to use, considering that the game’s concept involves having multiple maps with this feature. Regarding the possibility that the variable is not being destroyed when loading, I thought that was already happening in the GameManager class; I call the DontDestroyOnLoad function on the game object.

I’ve had weird results with DontDestroyOnLoad and changed to Additive mode too. Maybe you can set enemyToBringToBattle as static so that you don’t even have to worry about singleton or anything else.

You can also check if GameManager.instance is recreated after the scene load. It’s unlikely but your code has an issue so…

I will change to Additive mod too, i think works well, thank you so much for helping!

2 Likes