- Home /
Instantiate prefab (Resources.Load)
I know this question was asked many times, but I still cannot solve it...May I have some suggestions? What I want to do is to instantiate a prefab from resources folder, but it got "ArgumentException: The Object you want to instantiate is null." when instantiating. Here is my codes:
string path = "Prefabs/AnimatedCharacter/" + _theme+"/"+_wordID;
print(path); //printed: Prefabs/AnimatedCharacter/attraction/5C71
var animatedWordPrefab = Resources.Load(path) as GameObject;
var newWord = GameObject.Instantiate(animatedWordPrefab); //Get error
Target prefab is already placed inside the target folder.
I have no idea why I still getting wrong... Before the above code, I also use similar method to get the CSV, and it doesn't get wrong and work well.
string path = "CSV/" + _theme;
print(path);
TextAsset bookContent = Resources.Load<TextAsset>(path);
I also printed out _theme and _wordID, so I think the problem isn't related to _theme or _wordID...
Answer by tocu-adrian · Jul 24, 2020 at 06:32 AM
Hi, I reproduced your code and it's working:
private void Start()
{
string _theme = "attraction";
string _wordID = "5C71";
string path = "Prefabs/AnimatedCharacter/" + _theme + "/" + _wordID;
print(path);
var animatedWordPrefab = Resources.Load(path) as GameObject;
var newWord = GameObject.Instantiate(animatedWordPrefab);
}
In your case _theme and _wordID are string type ? Or how are you getting this values ? Maybe the problem is here, because if you give the path directly, the code works.
Omg...After I restarted the computer, it's work!! A$$anonymous$$AZING!! $$anonymous$$aybe sometime Unity cannot load resources (Although I tried to restart Unity, it seems that I should restart the computer...) Anyway, thank you very much for your suggestion!! Thank you!
Your answer
Follow this Question
Related Questions
Instantiated object not showing in scene or hierarchy 2 Answers
Load multiple instances of a prefab through code 2 Answers
Instantiating from an object classes GameObject property? 1 Answer
Cannot instantiate gameobject 3 Answers
Problem with missing texture with Instantiate( Resources.load ) method 0 Answers