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)
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)
}
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;
}