|
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.
(comments are locked)
|
|
Disable your objects instead of destroying them; reinitialize them and enable them rather than instantiating new ones. 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)
|
|
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#)
I would suggest that it is probably best to use this sparingly. The Awake method seems like the most appropriate place to load resources. 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)
|
|
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.
(comments are locked)
|
