Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
2
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.

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

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

Prefab destroyed upon instantiation 0 Answers

All instantiated objects having same location, 0 Answers

Unity won't Instantiate PLEASE HELP! 1 Answer

Create gameobject in other scene... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges