Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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

Discussion in 'Scripting' started by viniciusnazario25, Sep 3, 2023.

  1. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    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!
     

    Attached Files:

  2. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
  3. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    Thank you for the response; I'm already using the function. Please find the attached screenshot.

    imagem_2023-09-03_010814611.png
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I don't see DontDestroyOnLoad being called or the scene loaded in Additive mode here.
     
  5. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    My bad, wrong screenshot. I call the function in singleton
    Screenshot_3.png
     
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Ok my bad. You can probably debug this with a setter:

    Code (csharp):
    1.  
    2.     private GameObject enemyToBringToBattle;
    3.     public GameObject EnemyToBringToBattle
    4.    {
    5.        get {
    6.            return enemyToBringToBattle;
    7.        }
    8.        set {
    9.            Debug.Log($"setting {value} to enemy"};
    10.            enemyToBringToBattle = value;
    11.        }
    12.    }
    13.  
     
  7. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    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
    Screenshot_2.png
     

    Attached Files:

  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    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.
     
  9. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    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.
     
  10. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    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...
     
  11. viniciusnazario25

    viniciusnazario25

    Joined:
    Mar 8, 2019
    Posts:
    6
    I will change to Additive mod too, i think works well, thank you so much for helping!
     
    dlorre and wideeyenow_unity like this.