- Home /
Unity2D Instantiated Prefab Won't Appear in Scene
In our game, we have a prefab we want to insert into the scene in a C# script. It works fine when dragged and dropped into the script, but when we use this code:
GameObject prefab = Resources.Load<GameObject> ("Prefabs/StarDude");
Instantiate (prefab, new Vector3(0f, 2f, 0f), Quaternion.identity);
Then nothing appears in our scene when we run it. This is code is in the start function of a monobehaviour, could this be the problem? Can it not add to the scene while starting up? Should anything be appearing in the hierarchy when we run the game? Or are we missing something simple? We also tried making a public variable for the prefab and setting it in the inspector, and this didn't work either. Any help is appreciated.
Answer by Sildaekar · Mar 16, 2014 at 02:48 AM
Is the "Prefabs" folder located in a folder called "Resources"? Resources.Load can only load objects that are located in a Resources directory.
Loads an asset stored at path in a Resources folder.
Also, all prefabs have a suffix of .prefab so you should be loading StarDude.prefab instead of StarDude.
The folder is inside of the Resource folder, also according to the documentation, the extension should be omitted. "The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted."
The prefab seems to be loaded, because I can call it's methods, but it not appearing in the scene or the hierarchy.
Your answer
Follow this Question
Related Questions
Instantiate ground/enemies 1 Answer
Instantied 2D Prefab Is Invisible 1 Answer
Spawning prefabs dependant upon Health UI? 2 Answers
How to move Instantiated 2D objects by 0.5 using arrows 1 Answer