Before updating to Windows 10 you may need to return your Pro license. More information here

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?


more ▼

asked Feb 12, 2010 at 01:26 AM

avatar image

Jason Jolley
190 17 15 26

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

Jan 29, 2013 at 05:32 AM sdgd
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Feb 12, 2010 at 05:14 AM

avatar image

10.5k 78 127 171

Thanks! Works good.

May 05, 2010 at 05:55 PM Jason Jolley

nothing special!

May 06, 2010 at 08:54 AM Ashkan_gc

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

Oct 19, 2010 at 12:19 PM Cawas

Thanks. It is fully right answer. The other way to Find the object somewhere in scene... but it is not useful IMHO.

Jun 25, 2012 at 09:16 PM shaman4d
(comments are locked)
10|3000 characters needed characters left

public GameObject obj;

void Start () { GameObject myPrefab = (GameObject)Instantiate(obj); }

more ▼

answered Feb 12, 2010 at 01:38 AM

avatar image

121k 80 365 774

Thanks! I Appreciate the help :)

May 05, 2010 at 05:56 PM Jason Jolley
(comments are locked)
10|3000 characters needed characters left

Use Prefab in your Game Here is the tutorial for that . http://lnkd.in/J6ufVr . Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution ..

more ▼

answered Jul 13, 2011 at 10:34 AM

avatar image

0 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Feb 12, 2010 at 01:26 AM

Seen: 25396 times

Last Updated: Jan 29, 2013 at 05:32 AM