- Home /
Passing array of prefabs to next scene
I have two scenes. In one scene there are villages that have Level script attached. This script contains information about the level and most importantly prefabs of monsters that should be spawned for this level.
When a village is clicked it loads a second scene in which it should spawn monsters that were assigned to the village in the Level script. I don't know what is the best way to pass these prefabs from the village to the next scene to, where spawner will use them. What I did is:
I have a static variable, which is a reference to the Level script from the selected village.
public class ApplicationModel
{
public static Level selectedLevel = null;
}
When village is clicked it assigns the Level script to the static variable.
public class Level : MonoBehaviour
{
public GameObject[] easy;
public GameObject[] medium;
public GameObject[] hard;
public GameObject boss;
public int totalUnits;
public float spawnTime;
void OnMouseDown()
{
ApplicationModel.selectedLevel = this;
Application.LoadLevel("forest");
}
}
When a forest scene is loaded, in a different script I retrieve information and prefabs from the static selectedLevel
variable
public class LevelSceneManager : MonoBehaviour
{
public EnemySpawner spawner;
void Start ()
{
if(spawner != null && ApplicationModel.selectedLevel != null)
{
spawner.easy = ApplicationModel.selectedLevel.easy;
spawner.medium = ApplicationModel.selectedLevel.easy;
spawner.hard = ApplicationModel.selectedLevel.hard;
spawner.boss = ApplicationModel.selectedLevel.boss;
spawner.spawnTime = ApplicationModel.selectedLevel.spawnTime;
spawner.totalUnits = ApplicationModel.selectedLevel.totalUnits;
}
}
}
The problem is that ApplicationModel.selectedLevel
is null, which might be because the GameObject of that script was destroyed, but still I can access its variables e.g. ApplicationModel.selectedLevel.easy
is not null. While this approach works fine if I will remove if(ApplicationModel.selectedLevel != null)
statement, I'm concerned about memory leaks that it might cause. When you will go back to the world scene, all arrays from the previously selectedLevel
e.g. easy are still alive. What if you click a different village, will it destroy these arrays from the previous selectedLevel
or will they be kept somewhere in the memory? Is there any better solution to pass prefabs assigned to a script in the editor from one scene and access them in the other scene?
Answer by redeemer · May 24, 2015 at 11:44 PM
Maybe this ould help you. Why don't you try to mantain an object with the script where the info is?
You can do this just by using DontDestroyOnLoad() method. Just add to the Start or Awake method :
DontDestroyOnLoad(this.gameObject);
That will maintain the object passed as argument between scenes.
I leave you the link to the docs : http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
I shouldn't use DontDestroyOnLoad in Awake or Start method, because there are around 60 level objects in the scene, all of them would remain, while only 1 is needed. I guess I could call this method before loading a "forest" scene, but the question is will this object still remain even after loading back to the "world" scene from the "forest" scene?
I shouldn't use DontDestroyOnLoad in Awake or Start method, because there are around 60 level objects in the scene, all of them would remain, while only 1 is needed.
public static GameObject the$$anonymous$$eeper ;
void Awake()
{
if(!the$$anonymous$$eeper) the$$anonymous$$eeper = this.gameObject ;
DontDestroyOnLoad(the$$anonymous$$eeper) ;
}
There are a lot of ways to only call that method in the object you want, one of those is the above, as Lo0Nuhti$$anonymous$$ posted. Other options would be to do a script just for that object, or to invoke it depending on the tag... something like :
void Awake()
{
if(this.CompareTag == "ObjectTag") DontDestroyOnLoad(this.gameObject);
}
Will this object remain if I will load another scene? Let's say I call DontDestroyOnLoad method then I load the forest scene. Being in the forest scene I load another scene. Will this object still remain or will it be destroyed?
Yes, it will remain through all scene loads until you destroy it manually
Your answer
Follow this Question
Related Questions
Prefab is completly messed up 2 Answers
Passing a prefab between scenes? 1 Answer
Prefab looks different in different scenes 0 Answers
How to save all scene? 1 Answer
How do I select a prefab that was just instantiated from an editor window? 1 Answer