- Home /
Instantiate Prefab
How do you instantiate a Prefab in C#? I've been doing it by loading a resource and then casting it into a UnityEngine.Object:
UnityEngine.Object MyPrefab = Resources.Load("MyPrefabl");
Is there a way to do it without using the Resources.Load?
Thanks!
yeah but that object "ball" becomes it self when instantiated and not prefab
and thanks for your Question now I know how to Instantiate Object so it has Prefab in it and not it self
Answer by Ashkan_gc · Feb 12, 2010 at 05:14 AM
define a public variable of type GameObject in your component (script) and then in the inspector drag a prefab on it and you can instantiate it.
public class script1 : MonoBehavior
{
public GameObject ball;
void Start ()
{
GameObject myball = (GameObject)instantiate (ball);
}
}
when you attach this small script to a gameObject it will add script1 component to it's inspector and the script1 component has a slot called ball. you should drag a prefab to it and then the code will instantiated that dragged prefab.
and I suppose this is how you can instantiate the prefab all within the script only, without draggin it in the inspector: http://answers.unity3d.com/questions/17425/instantiate-prefab-wihtout-dragging-prefab-into-gameobject-script
Thanks. It is fully right answer. The other way to Find the object somewhere in scene... but it is not useful I$$anonymous$$HO.
Answer by Eric5h5 · Feb 12, 2010 at 01:38 AM
public GameObject obj;
void Start () { GameObject myPrefab = (GameObject)Instantiate(obj); }
Your answer
Follow this Question
Related Questions
How can I instantiate more prefabs as the score increases? 2 Answers
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer
Prefabs instantiated from an array are keeping their public int value 1 Answer
Prefab script to effect the unique instance 1 Answer
Help making a damage number system 1 Answer