Weird issue with Singleton
Hello everyone. Im quite new to Unity and C#. Been programming For Android for the passed few years.
I have a question about an issue Im encountering in my singleton class.
I have a Survival type game where you run around in a map and you shoot spawned enemies. To build the map I have a MapGenerator class. This class creates the map + obstacles. It also has a method that returns an empty cell (there is no obstacle in that cell).
I also have a Powerup mechanism that drops in a powerup at specific times. That happends using a PowerUpManager which is a singleton. That singleton has a method called DropHealthPowerup(). What it does first, is obtaining an empty cell using the MapGenerator and then it instantiates a 'full health' prefab in that cell. So to test this, I assigned a hot key 'J' to drop a health power up. It works perfectly.
Then, when the player dies somewhere in the game, I have the options to PLAY AGAIN. This button basically calls a method that calls Application.LoadLevel("main01"). This essentially loads the game again. But now, it seems, the MapGenerator object is null even though I do FindObjectOfType in the Awake method. When I debug this, in the Awake method _mapGenerator has a reference to a class. But when I press 'j' and the DropHealthPowerup() method gets called, _mapGenerator is null. It seems the _mapGenerator object inside the DropHealthPowerup() method is not the same object as in the Awake method. I can only assume the Destroy(gameObject) in the if statement in Awake has something to do with this mixup. This is the code for the singleton class:
 using UnityEngine;
 using System.Collections; 
 using System.Collections.Generic;
 public class PowerupManager : MonoBehaviour {
     public static PowerupManager _instance = null;
     [Header("Power ups")]
     public GameObject _healthPowerup;
     private MapGenerator _mapGenerator;
     void Awake(){
         if (_instance != null) {
             Destroy(gameObject);
         } else {
             _instance = this;
             DontDestroyOnLoad(gameObject);
         }
         _mapGenerator = FindObjectOfType<MapGenerator>();
     }
     public void DropHealthPowerup(){
         Vector3 fixedPosition = _mapGenerator.GetRandomOpenTile().position;
         fixedPosition += Vector3.up * 0.7f;
         Destroy(Instantiate(_healthPowerup, fixedPosition, Quaternion.identity), 5);
     }
 }
Any ideas what might be the issue? Thank you!
Your answer
 
 
             Follow this Question
Related Questions
I have gotten all these errors when trying to script ? 2 Answers
Prevent gameobject go off screen 0 Answers
joystick android 3d , help. 0 Answers
How to make scoring system 1 Answer
Exporting Maya Animation of player with ball to unity ? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                