x


How can I reference a prefab from a gameobject instance of it.

I have a script that finds a set of gameobjects in the scene and destroys them. (I do not know in advance which specific objects they are.) I then want to instantiate those objects after they have been destroyed. The references to the gameobjects are null (since they're destroyed) so I need to identify the prefabs they were based on.

more ▼

asked Mar 01 '10 at 03:10 AM

Frankie Mac gravatar image

Frankie Mac
36 1 1 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Disable your objects instead of destroying them; reinitialize them and enable them rather than instantiating new ones.

more ▼

answered Oct 24 '10 at 03:17 AM

Loius gravatar image

Loius
10.6k 1 11 41

How to reinitialize an Object into its prefab's default values ?

Jan 09 '11 at 09:09 PM Agustin Petrini

There's no function to do it, you have to do it manually.

Jan 10 '11 at 03:49 AM Loius
(comments are locked)
10|3000 characters needed characters left

I have just started to use Unity and this was the first thing I wanted find out myself. There is a way to do this via the Resources.Load() method.

Firstly, create a new folder called Resources anywhere in your Unity Project. This folder can have subfolders. Now move your Prefab to this folder. Supposing that your Prefab is called "MyPrefab.prefab" you would do this (in C#)

private GameObject TestPrefab;

void Awake() 
{
    TestPrefab = (GameObject)Resources.Load("MyPrefab", typeof(GameObject));
}
void Update () {

    if(Input.GetKeyDown("space"))
    {
        Instantiate(TestPrefab,new Vector3(0,0,0),Quaternion.identity);
    }
}

I would suggest that it is probably best to use this sparingly. The Awake method seems like the most appropriate place to load resources.

more ▼

answered Oct 23 '10 at 11:40 PM

boymeetsrobot gravatar image

boymeetsrobot
587 7 10 22

This approach works beautifully when trying to manage resources yourself, let's say in a non behavior script where you cannot attach those prefabs

Mar 20 '12 at 08:07 PM ate

+1 Amazing! I wish I could upvote your answer.. but I'm still a newbie apparently. The "Resources.LoadAll()" function is also quite nice. It keeps you from having to manage a list of desired assets and can be a lifesaver in some situations.

Jan 04 at 01:16 PM Mantic
(comments are locked)
10|3000 characters needed characters left

unfortunately there is not any other way. you should create a public GameObject variable for each of those GameObjects and set that variable to the prefab in inspector. you can not find or get reference to prefabs by any means at runtime. (this is a big limitation that should be removed). keep in mind that when you say this.gameObject it will give you a reference to current instance so after distruction it will becom null.

more ▼

answered Mar 01 '10 at 05:31 AM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(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:

x2077
x1670
x1253

asked: Mar 01 '10 at 03:10 AM

Seen: 8523 times

Last Updated: Jan 04 at 01:17 PM