Hey! I’m just hoping for some advice with loading prefabs at the start of a level.
My issue first appeared when I started my game. I have multiple cubes, when one collides it is destroyed and another gameobject is put in its place for a few seconds which shows the points score of the cube and a particle system etc for a few seconds. The problem was that the very first cube to be destroyed would cause the game to freeze for around 1/4 second, I’m guessing whilst the new gameobject loaded initially, because every cube after it would not cause any lag at all.
To fix this I created a loading script, with an array of various prefabs I want to load at the start of the scene. In the script, each prefab is instantiated in the scene, and then destroyed. When this has been done with all prefabs the game can begin.
As long as this loading script runs before I start playing, I encounter no lag during the game for the first cube, or any others after it.
So… this solved my problem. But my question, why I’m posting on here, is to ask whether this is a good approach? Is this what everyone else would do to load a list of prefabs into memory to prevent lag, or is there another method I should look at that would be better? I could just get on with it now everything is working but as this was just me thinking up a method, and I’m very new to Unity I thought I would check in case there is a much easier, or better method.
You are stating that you have to instantiate the prefab to have it load into memory? That is not a necessary approach. The prefab should already have been loaded into memory if you have the possibility to instantiate it.
A prefab gets loaded into memory at the start of the game if the prefab is referenced by a public variable of some sort. Nothing more is needed then to load it. For example like this:
public class Test : MonoBehaviour
{
public GameObject prefab; //The prefab is already loaded here
public void CreateObject()
{
Instantiate(prefab); //This should not cause a lag except for big big prefabs
}
}
If you are not storing the references before the start of the game, then maybe you are using Resources.Load to load the prefabs? The prefabs will be loaded into memory before you instantiate them then as well:
public class Test : MonoBehaviour
{
public string prefabLocation; //path to prefab
private GameObject loadedPrefab;
private void Start()
{
loadedPrefab = Resources.Load(prefabLocation) as GameObject; //The prefab gets loaded here, so any lag should happen here as well
}
public void CreateObject()
{
Instantiate(loadedPrefab ); //This should not cause a lag except for big big prefabs
}
}
In conclusion: as long as you use Resources.Load and load your prefabs at the start of the game, you should not have to instantiate them as well to have them loaded.
Yes, people generally do it this way. Make a public variable of GameObject or Transform. Make a reference of the prefab that you want to Instantiate. But of course scenario changes with each and every game. If you wish to load a whole bunch of data at a time, you might consider saving it as a new level and loading it with Application.LoadLevelAsync() at a proper time.