How to load sprites from other scenes?

Updated on August 31, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on August 31, 2018

I would like to split each of my levels into separate scenes.

Now I would like to know how do you load those scenes (i.e. levels) into the main-scene? I just ask because I’d like to know how to load sprites from other scenes into my main-scene (if this is possible). In my main-scene the prefabs should be instantiated when loading is finished.

Do I have to get the sprites via Resources.Load? E.g. like this:

 public List<GameObject> items = new List<GameObject>();

 void Start() {
 GameObject sprite = null;
 int counter = 0;
 bool done = false;
 while(!done)
 {
     sprite = Resources.Load("Item" + counter) as GameObject;
     if(sprite == null) {
         done = true;
     } else {
         items.Add(sprite);
 }
     ++counter;
 }
}

Or shall I instantiate like this (with the help of an array):

public class LevelLoader : MonoBehaviour {

public enum Level {
    Level1,
    Level2,
Level3
}

public Object[] levelPrefabs;

public void Load(Level level)
{
    int levelIndex = (int)level;
    if (levelIndex >= 0 && levelIndex < levelPrefabs.Length) {
        Instantiate(levelPrefabs[levelIndex]);
    } else {
        Debug.LogError("Invalid level index: " + levelIndex);
    }
}
}

My knowledge of C# is limited.

  • Liked by
Reply