I have bunch of managers, like GameManager, LevelManager, UIManager, etc.
All of them declare a public static variable of their class type and call it “instance”. Then all of them have this code in their Awake
void Awake
{
if (instance != null)
Destroy(gameObject);
instance = this;
DontDestroyOnLoad (gameObject);
}
Basically they’re singletons so that I can reference them easier.
When I’m trying to load or reload the level, I get this error: “The object of type ‘GameObject’ has been destroyed but you are still trying to access it.”
All my managers are in DontDestroyOnLoad section of the scene, i.e. the gameobject and the scripts on them are preserved. I don’t understand why the hell am I getting this error when I reload the scene and try to access these statics.
Can someone make sense of this?
EDIT:
@hexagonius @jackishere @RobAnthem @ExtinctSpecie
I think the problem here may be deeper since out of all those managers, only a specific one is throwing that error and i have problem debugging it. here’s what’s happening:
The loader is instantiating all the managers like so
void Awake ()
{
if (LvlGenerator.instance == null)
Instantiate(lvlGenerator);
if (GameManager.instance == null)
Instantiate(gameManager);
//etc.
}
then GameManager is calling this method upon being instantiated
void SetUpNewScene()
{
SceneManager.LoadScene("Scene1");
LvlGenerator.instance.GenerateLevel();
UIManager.instance.SetUpDeathScreen();
}
*here if I omit "SceneManager.LoadScene(“Scene1”) then the entire thing works fine. It’s the loading that screws it. *
UIManager for example works fine, and LvlGenerator isn’t null either. It goes through and calls GenerateLevel() method. Which looks like this
public void GenerateLevel()
{
transform.position = Vector3.zero; //moves LvlGenerator to 0,0,0 so that we start building from those coordinates
StartCoroutine(GenerateLevelCoroutine());
}
then it starts the coroutine which is quite some code, I’ll cut out most part and leave the one that throws the error (which I don’t understand).
IEnumerator GenerateLevelCoroutine()
{
int floorTilesRemaining = levelSize;
SpawnFloorTile ();
floorTilesRemaining--;
while(floorTilesRemaining > 0)
{
if (floorTilesList.Count > 0)
{
for (int i = 0; i < floorTilesList.Count; i++)
{
if (transform.position == floorTilesList *.transform.position)*
-
{*
// some code
-
}*
-
}*
-
SpawnFloorTile ();*
-
floorTilesRemaining--;*
-
}*
yield return null;
}
}
THIS line “if (transform.position == floorTilesList _.transform.position)” is throwing an error mentioned above (The object of type ‘GameObject’ has been destroyed but you are still trying to access it.)._
When I try to debug it, none of the variables there are null or anything.
floorTilesList is declared in the same script as the error
public List floorTilesList = new List();
I still don’t understand why this is happening : (