- Home /
Instantiate PreFab with C# Script
Hi,
I've found online that the best way to instantiate a game object type is with the following statement:
GameObject go = Instantiate(Resources.Load("MyPrefab"));
This does not work for me, does it require more using statements than:
using UnityEngine; using System;
I am getting the following error:
Assets/Scripts/Instantiate.cs(54,67): error CS0426: The nested type Load' does not exist in the type
UnityEngine.Resources'
Besides the spelling mistake with Resources (which the compile error implies is not a problem with the original code), it looks fine - could you post the actual code?
Right, overlooking the spelling mistake, this is the actual code (C#): GameObject go = Instantiate(Resources.Load("$$anonymous$$yPrefab"));
uh... Instantiate.cs.... that can't be good.... try rena$$anonymous$$g that file or using Object.Instantiate for all instantiations from now on
That didn't work either, nor did having my script under the Resources folder, with the prefabs.
$$anonymous$$y goal, is to get a particle effect happening for the player, when I press a key on the keyboard.
Answer by Ashkan_gc · Jun 10, 2010 at 11:31 AM
you can create any public variable type that is a children of component or GameObject and instantiate it with this function. the return type of Instantiate is UnityEngine.Object and you should cast it if you want to asign it to a variable of type GameObject. so just (GameObject) before the Instantiate and it will work well. you should have the resources folder with the prefab in it thow.
GameObject go = (GameObject)Instantiate(Resources.Load("MyPrefab")); ;
or
GameObject go = Instantiate(Resources.Load("MyPrefab")) as GameObject;
see MSDN topics for as keyword and topics about casting for more information.
I am using Unity Free edition and resources work fine for me.
the faster way is to make a public variable for the Prefab and drag your Prefab on it in the inspector and then instatiate it. Resources.Load is really slow, so you should avoid it, if you can.
public GameObject myPrefab;
GameObject go = (GameObject)Instantiate(myPrefab);
Answer by _Petroz · Jun 10, 2010 at 10:58 AM
In the root of your Project hierarchy make a folder called 'Resources', put your prefab in that folder. My prefab is called DamageText and here is my working code:
GameObject textObject = (GameObject)Instantiate(Resources.Load("DamageText"));
Tested using C# with Unity 2.6.
this is the actual correct answer. Resource.Load looks in a resource folder to find the object you want to load. The error message tells you this as well (in a very confusing way):
Assets/Scripts/Instantiate.cs(54,67): error CS0426: The nested type Load' does not exist in the typeUnityEngine.Resources'
Resource.Load also works if the Resource-Folder is not on top of your Project or Asset dir, it can by within any subdirectory if you want, i.e. Assets/Prefabs/Weapons/Resources/myPrefab is perfectly fine.
Answer by user-1846 (google) · May 26, 2010 at 11:55 PM
This is what I would do
at the top put
public Transform whatYouWantToInstantiate;
then wherever you want to instantiate
Instantiate(whatYouWantToInstantiate);
That's the simple way...
Christian Stewart
This way work for a transform varaible, but I need to instantiate a prefab. Sorry I left that unspecified.
it should work the same - you can just drag the prefab onto the transform variable
Answer by mocaponeuk · Nov 17, 2013 at 07:05 PM
im getting 'The variable `go' is assigned but its value is never used'
That's because you are creating a new GameObject called "go" and you are making that gameObject by instantiating. The reason it says thatb it's never used because you probably don't have anything like go.GetComponent or any other thing that you want to do with go, therefor it's assigned but you didn't use that assignment... If you get what I mean
Answer by TheRealKuha · May 18, 2016 at 07:27 PM
I found a video explaining this pretty good if you still need help, also tells you how to spawn a rigidbody: https://www.youtube.com/watch?v=_Xrw2EEhzI4