DontDestroyOnLoad() and GameObject.FindGameObjectWithTag() [C#]
Hey everybody ! I have an issue with DontDestroyOnLoad : In the First Scene, the payer have the possibility to enter in a battle, which take place in an other scene. In order to save all mobs in the battle and some other things, I use a GameObject with this code :
     public string currentPlayerScene;
 
     public float xPlayerPos;
     public float yPlayerPos;
 
     public List<GameObject> allAllies;
     public List<GameObject> allEnemies;
 
     void Awake()
     {
         DontDestroyOnLoad(transform.gameObject);
     }
When the BattleScene is loaded, i have a script, named "BattleManager". In this code, there is a public GameObject for the DontDestroyOnLoad GameObject which is assigned by "GameObject.FindGameObjectWithTag("BattleSettings)" in the Start method.
The problem is : the variable isn't assigned, so all the other methods can't be used.
This is the Start Method of "BattleManager" :
 void start()
     {
         enterBattleSettings = GameObject.FindGameObjectWithTag("BattleSettings");
 
         currentPlayerScene = enterBattleSettings.GetComponent<BattleSettings>().currentPlayerScene;
 
         xPlayerPos = -10;
         yPlayerPos = 10;
 
         allAllies = enterBattleSettings.GetComponent<BattleSettings>().allAllies;
         allEnemies = enterBattleSettings.GetComponent<BattleSettings>().allEnemies;
 
         EnterBattle(enterBattleSettings.GetComponent<BattleSettings>().allAllies, enterBattleSettings.GetComponent<BattleSettings>().allEnemies);
     }
Thank you !
Bye, xyHeat
Answer by xyHeat · Aug 13, 2016 at 10:52 AM
Hey, I found something : I replace the Start method by a OnLevelWasLoaded() method. It seem work.
Bye, xyHeat
I have the same problem and I saved my gameObject in a variable and when i use that variable, it shows Null Reference exception. I don't know how to solve it.
$$anonymous$$y query got solved. DontDestroyOnLoad() only works for root object. $$anonymous$$y GameObject was the child and hence I was not able to access it in OnLevelWasLoaded().
Your answer
 
 
             Follow this Question
Related Questions
Instantiate as child for gameobjects with tag 1 Answer
how to disable dontdestroyonload on a specific scene 2 Answers
Destroyed instance of Prefab, can't spawn it back. 1 Answer
How to put ".transform" after "GameObject.FindGameObjectsWithTag()" 1 Answer
"Field " " is never assigned to, and will always have it's default value null" 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                