- Home /
Can't load prefabs from resources into an array
I'm making a game with very small levels (think Wario Ware Style). I decided that every microlevel should be a prefab. So I'm going to put all the level prefabs into a Levels directory inside my Resources folder. Then I'm planning to load all the levels into an array, shuffle it with a simple algorithm and then spawn one of them at a time. (I've got an external manager calling the spawn function when needed). Problem is, it seems not to load my levels at all. Is there something wrong in what I'm doing? And plus, is this a good approach? Thank you!
private Transform _currentLevel;
private Transform _player;
private Transform[] _levels;
private int _levelIndex;
// Use this for initialization
void Start () {
_player = GameObject.FindGameObjectWithTag("Player").transform;
//load all levels from resources
_levels = Resources.LoadAll("Levels") as Transform[];
_levelIndex=0;
}
public void SpawnLevel ()
{
if (_levelIndex==_levels.Length-1)
{
_levelIndex=0;
MixLevels ();
}
_currentLevel=Instantiate(_levels[_levelIndex],transform.position,Quaternion.identity) as Transform;
_currentLevel.parent=this.transform;
_player.position=new Vector3(0,1,0);
_player.gameObject.SetActive(true);
_levelIndex++;
}
public void DestroyLevel ()
{
_player.gameObject.SetActive(false);
if (_currentLevel) Destroy(_currentLevel.gameObject);
}
public void MixLevels()
{
//apply a Fisher-Yates algorithm to create a new level pool
for (int i = _levels.Length-1; i>0; i--)
{
var r = Random.Range(0,i);
var tmp = _levels[i];
_levels[i]=_levels[r];
_levels[r]=tmp;
}
}
}
Looks like that. For example, if I do a Debug.Log(_levels[0].name); I get a NullReferenceException error. If I do a Debug.Log(_levels) I get a Null. The array looks empty to me :/
So obviously you're not getting anything back from Resource.LoadAll, have you tried loading each level individually in a loop? levels[i] = Resource.Load("Level" + i) as Transform;
I wouldn't go for this approach, why loading everything all at once and then use whenever you need? - just load whenever you need, I would use a yield return 'level_I_need' for that.
The problem with loading just one level at a time is that way I have to know exactly how many microlevels are in the game. What I need is something that could adapt if I decide to add new levels. By loading all of them I'm sure I always get the array with the right dimension.
Actually, the idea of loading everything from resources was just to let me add new levels as I'm working, without having to resize arrays or add them manually to a list everytime I create a new one :)
BTW what exactly do you mean to use a yield for that? I'm confused, can you elaborate? Thanks!
And if you try the load without casting to Transform[]? and save them in an Object[] array? Does it still stay empty?
Answer by Joyrider · Aug 13, 2013 at 09:43 AM
This should work :
Object[] _levelObj = Resources.LoadAll("Levels", typeof(Object));
my guess is, Unity doesn't like your array casting as Transform[]
Oh, it seems the casting was the problem. Now it works (almost) perfectly. I've still got a problem with the instantiate part, it correctly spawns the level, but somewhat it cannot parent it to my gameobject (it throws the same Nullreference Exception). Let's see if I can solve this.
After you loaded your levels as objects, did you convert each one again to 'Transform'?
Solved, still a transform casting problem. I casted to a GameObject ins$$anonymous$$d and everything works now, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613112109im_/https://answers.unity.com/themes/thub/images/avi.jpg)