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.

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.

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.

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

For those who want, like me, to get prefab of an object that is already in the scene, in editor mode (I’m doing a level editor), here is what I use :
(needs “using UnityEditor;”)

GameObject thePrefabOfTheGameObjectAlreadyInTheScene = PrefabUtility.GetPrefabParent(gameObject));

That saved my day. So I Hope that it will help.

When you instantiate the Prefab, have a static gameobject reference to it.
So you need to do it like this:
public static GameObject TestPrefabRef;

public GameObject TestPrefab;

if(input.GetKeyDown("space"()) {
    TestPrefabRef = (GameObject) Instantiate(TestPrefab, new Vector3(0,0,0), Quaternion.identity);
}

The static gameobject is what you use with other scripts so it would be:
ScriptName.TestPrefabRef