• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Savbacsi · Dec 06, 2015 at 03:21 PM · gameobjectinstantiateprefabobjects

How to find the (original) Prefab of a GameObject?

I have a lot of GameObjects in my Scene (different type of enemies, environment objects more than 100). I have collected all of them to a list. Sometimes these gameobjects are destroyed as the game flow. How to restore them? My idea is to find the GameObjects ID or reference to it's prefab. Save to a list and Instantiate but can't find the Prefab via script. (Drag and Drop is impossible. Too many of them)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alebasco · Dec 09, 2015 at 09:43 PM

Getting access to a prefab, from an instantiated object is an editor only functionality - I.E. it will only work at edit time, not run time.

There are other solutions to your issue though.

Don't destroy the objects. If you disable them instead, you can renable them later

 void DisableGameObject(GameObject objectToDisable)
 {
     objectToDisable.SetActive(false);
 }
 
 void EnableGameObject(GameObject objectToEnable)
 {
     objectToEnabled.SetActive(true)
 }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GioVil · Dec 16, 2015 at 02:19 PM

Firstly sorry for my english. If you want you can add a property of the original prefab on the instance in a script. For Example you can create a script with a enumerator

using UnityEngine;

using System.Collections;

public class PrefabInstance : MonoBehaviour {

 public enum PrefabProperty {FirstPrefab, SecondPrefab, ThirdPrefab};
 public PrefabProperty prefabProperty;

}

and add it to the instance

using UnityEngine; using System.Collections;

public class InstantiatePrefab : MonoBehaviour {

 public GameObject prefab;
 public List<PrefabInstance> instances = new List<PrefabInstance>();

 // Use this for initialization
 void InstantiateSomePrefabs () {
     GameObject obj = GameObject.Instantiate(prefab) as GameObject;

     PrefabInstance instance = obj.AddComponent<PrefabInstance>();
     instance.prefabProperty = PrefabInstance.PrefabProperty.FirstPrefab;
     instances.Add(instance);
 }

}

for search all game objects whit the same property

using System.Linq;

 public PrefabInstance[] InstancesFromFirstPrefab(){
     PrefabInstance[] tmpInstances = instances.Where(d => d.prefabProperty == PrefabInstance.PrefabProperty.FirstPrefab).ToArray();
     return tmpInstances;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with Prefab instantiate and script not working? 0 Answers

Place prefab with mouse on to a grid based surface with a correct orientation. c# 0 Answers

Prefab destroyed upon instantiation 0 Answers

Unity won't Instantiate PLEASE HELP! 1 Answer

What is Casting in Unity ? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges