- Home /
Resources.Load in dictionary
I have a static class that handles all the weapons and inside a dictionary that stores the weapons
a part of the code:
public static Dictionary<string, Weapon> weapons= new Dictionary<string, Weapon>() {"Pistol",new Weapon(){prefab = Resources.Load<GameObject>("My pistol");}};
For some reason it works as intended in the editor but when I make a build of the game it gives this error:
"ArgumentException: Load can only be called from the main thread.
Constructors and field initializers will be executer from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code in the Awake or Start function."
I can't use Awake or Start beacause the class doesn't inherit from MonoBehaviour.
Answer by Tuncer · Jan 12, 2016 at 11:25 AM
Create a class to load your resources,
public class ResourceLoader : MonoBehaviour
{
public static GameObject pistolPrefab;
private Awake()
{
pistolPrefab = Resources.Load<GameObject>("My pistol");
}
}
Then, you can use it :
public static Dictionary<string, Weapon> weapons= new Dictionary<string, Weapon>() {"Pistol",new Weapon(ResourceLoader.pistolPrefab)};
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I send a message to every entry in a dictionary? 1 Answer