[Solved] Error: LoadAll is not allowed to be called from a MonoBehaviour constructor

I am writing a script for loading resources I need when starting the scene.
I am trying to use Resources.LoadAll and then make the array as static variable
so that I could use them in other scripts.

using UnityEngine;
using System.Collections;

public class ResourceManager : MonoBehaviour {

    public static GameObject[] buff;
    public static GameObject[] orelist;
    public static GameObject[] terrainlist;
    public static GameObject[] playerlist;
    public static Sprite[] playerSprite;


    void Awake () {
        buff = Resources.LoadAll<GameObject>("Buff");
        orelist = Resources.LoadAll<GameObject>("OreTile");
        terrainlist = Resources.LoadAll<GameObject>("TerrainTile");
        playerlist = Resources.LoadAll<GameObject> ("Player");
        playerSprite = Resources.LoadAll<Sprite>("Sprite_Player");
    }

}

and then about 10 errors similar to this pop up:

I have been searching for solutions for some days but I have no clue what I did wrong.
Please help if you get any ideas!

The error references a class called PlayerController. What does that look like?

2 Likes

The error is due to the following problematic statement in my Player Controller script

SoundManager sound = new SoundManager ();

and then I call the function sound.step
but in my SoundManager script, I included LoadAll function in the Start function
and probably that causes the error.

Thank you for KelsoMRK’s help. I shift the attention to the PlayerController script, instead of just looking for LoadAll function.

1 Like