x


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!

more ▼

asked Feb 12, 2010 at 01:26 AM

Jason Jolley gravatar image

Jason Jolley
180 17 15 23

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

Ashkan_gc gravatar image

Ashkan_gc
10.1k 77 104 165

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

Eric5h5 gravatar image

Eric5h5
112k 56 203 694

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

vivek2012 gravatar image

vivek2012
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

By RSS:

Answers

Answers and Comments

Topics:

x2957
x2151

asked: Feb 12, 2010 at 01:26 AM

Seen: 24734 times

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