Home > OS >  Losing references after reloading the scene, even though they exist again
Losing references after reloading the scene, even though they exist again

Time:12-06

So basically im creating a space sandbox game on mobile where you can create your own universe. At the start of the game you can choose your rocket. Every rocket has its own Camera, Player and Spawner attached to it. Not the nicest solution but it works. After the player chooses his rocket, I destroy the spawners on the inactives ones, so they dont work. My problem is when exiting the scene and choosing a different rocket and repeating the process, the SpawningManager with all the references to all Spawners lost the references to them. Even though I reloaded the scene all references are still lost. Sorry if this was hard to follow but it was hard to explain.

Here is a part of the code that destroys the spawners(represented by colors)

if (BlueSelected == true)
        {
            Destroy(RedSpawner);
            Destroy(OrangeSpawner);
            Destroy(YellowSpawner);
            Destroy(GreenSpawner);
        }

And a part of one of the planet spawner(represented by colors)

{ public Transform SpawnPointBlue, SpawnPointRed, SpawnPointOrange, SpawnPointYellow; public GameObject Mercury;

// Checks if the Spawner is greater than null (There or not) and Instantiates the Object at the Players Position
public void SpawnMercury()
{
    
    if(SpawnPointBlue != null)
    {
        Instantiate(Mercury, SpawnPointBlue.position, SpawnPointBlue.rotation);
    }

    if(SpawnPointRed != null)
    {   
        Instantiate(Mercury, SpawnPointRed.position, SpawnPointRed.rotation);
    }

    if(SpawnPointOrange != null)
    {   
        Instantiate(Mercury, SpawnPointOrange.position, SpawnPointOrange.rotation);
    }

    if(SpawnPointYellow != null)
    {   
        Instantiate(Mercury, SpawnPointYellow.position, SpawnPointYellow.rotation);
    }

CodePudding user response:

If I understood well, your SpawnerManager is in "don't destroy on load" and keep existing between scenes. When you start your game you have referenced manually the spawners to the manager and everything is fine then when you go to another scene and come back where the spawners should be, there is no more references into the manager ?

If so, this seams normal to me, the references between scenes are not supported and when a scene is unloaded, the references disappear.

If I misunderstood something I would be glad to dig for the solution a bit

  • Related