- Home /
how to instantiate prefab with Resources.Load ?
I've went several times through this Unity Manual it's always missing as GameObject on end and even if I correct that error it always thinks my instance is Null how do I instantiate a Prefab? so I don't need to duplicate every prefab?
what I want to achive is: Prefab in instantiated Object and not GameObject as him self
GameObject instance = Instantiate(Resources.Load("Hatchet"));
GameObject instance = Instantiate(Resources.Load("Hatchet", typeof(GameObject))) as GameObject;
ArgumentException: The thing you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:71)
UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:58)
Inventory.Start () (at Assets/Player/Inventory.cs:38)
What problem are you trying to solve by loading the prefab from Resources?
yes so it understands what's prefab when it's in scene so it doesn't point to it self but prefab
$$anonymous$$ake sure the prefab is in a folder called "Resources", in your asset folder. Not an answer, as you probably did it already.
Hi, You can take a look at PrefabUtility.InstantiatePrefab. That could helps you.
@$$anonymous$$ikilo a Resources.Load (that works) creates a copy of the resource loaded (they aren't cached), then Instantiate would make a second copy. We can only see a bit of the OPs code, but if that happened more than once it would waste resources.
Answer by appearance · Jan 29, 2013 at 11:45 AM
Make sure that "Hatchet.prefab" file exists in Resources folder and 'Resources' folder exists in 'Assets' folder. It should work then.
finally thanks
ammm... next problem if I end up with 500 prefabs that needs to be created and deleted and read the deleted prefab witch I can now
how can I keep track of them only in 1 folder but thanks a bunch for now
it works I didn't have Resources folder in my unity project
not even the smallest logic didn't brought me there
c:\Program$$anonymous$$g\unity projects\crafting house\Assets\Resources\
You can store your prefabs in subfolders inside 'Resources' folder. (Ex: Assets/Resources/category1/hatchet.prefab). In this case you can load it as Instantiate (Resources.Load ("Category1/Hatchet") as GameObject);
You can create as many folders / sub-folders inside 'Resources' folder to maintain your prefab hierarchy.
Instantiate (Resources.Load ("Category1/Hatchet") as GameObject);
Should be
Instantiate (Resources.Load ("Category1/Hatchet")) as GameObject;
lol this is the best answer :D I tried to change my code, looked into documentation, tried unity.object type but the problem was that I moved wrong prefab to resources folder so script couldn't find it
Answer by komal2992 · Jan 25, 2017 at 01:05 PM
make sure your folder name is Resources. then create a prefab...
Answer by aliffhatim · Nov 21, 2018 at 09:40 AM
Hi sorry for asking can u solve my problem , i think almost same problem as him, this is the error code
ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:381) UnityEngine.Object.Instantiate (UnityEngine.Object original) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:240) Sample.ImageTarget_DynamicLoad_ManualPlay.LoadVideo () (at Assets/EasyAR/Scripts/ImageTarget_DynamicLoad_ManualPlay.cs:40) Sample.ImageTarget_DynamicLoad_ManualPlay.Start () (at Assets/EasyAR/Scripts/ImageTarget_DynamicLoad_ManualPlay.cs:35)
The code on the line are respectively: cs 35 . LoadVideo() cs 40 . GameObject subGameObject = Instantiate(Resources.Load("TransparentVideo", typeof(GameObject))) as GameObject;
how can I solve this. Thank You.
Answer by Volorf · Jul 15, 2020 at 07:38 PM
Try to preload the prefab before you instantiate it. You can use Start() method.
GameObject _go;
void Start()
{
// Make sure that you put your prefab in the folder Assets/Resources
_go = Resources.Load("MyPrefab") as GameObject;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(_go, transform.position + Vector3.up, Quaternion.identity);
}
}
Your answer
Follow this Question
Related Questions
Can't set the random position for an Instance. 1 Answer
prefab null not instantiated: simple example not working? 2 Answers
Follow and Instantiated Object 0 Answers
Check if object is destroyed on level load, if so instantiate prefab? 1 Answer
how to instantiate object with content prefab and not him self 2 Answers