- Home /
Losing references after reloading the scene, even though they exist again.
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);
}
Answer by KittenSnipes · Dec 05, 2021 at 12:48 AM
So why destroy these spawn points in the first place? I recommend just setting them inactive and when needed set them active again.
All you need to do is get the gameobject of these spawners and then use the function to turn them off here is an example:
//This sets the object inactive
GreenSpawner.gameObject.SetActive(false);
//This sets the object active
GreenSpawner.gameObject.SetActive(true);
Oh yeah, thanks a lot for your reply! Looking back im not sure why I wanted to destroy them either. This solution kinda just went over my head. I just read the unity documentation about SetActie so hopefully I wont run into there problems again. Thanks a lot for the help =)
Your answer
Follow this Question
Related Questions
how to destroy a object only in game and not the prefab 2 Answers
How Do I Add An Instantiated Object To An Array? 3 Answers
Pass a copy of a GameObject as variable to another script? 1 Answer
Problems with spawning items that the players can pick up - Unet 0 Answers
How do I spawn an object under another moving object at random in C#? 1 Answer