Is there a way for an Instantiated object to know what object created it?


I have a game object that instantiates a prefab. I have the following for instantiating a prefab;

public GameObject ene_foot_prefab; // enemy prefab
private GameObject obj;

private OnEnable(){
obj = (GameObject)Instantiate (PodPrefab, m_podLocation, Quaternion.identity);

I have the obj because it makes destroying the prefab easier. The object is instantiated when the player collides with a collider box, which toggles the spawner’s active property, thus the OnEnable function.

What I want to know is whether it is possible for the instantiated object to know what game object created it? I was thinking of setting the object’s parent to the spawner object but I don’t know where to go from there.


This will parent the obj to the gameobject that contain the script

obj.transform.parent = transform.parent;

Right after that line

obj = (GameObject)Instantiate (PodPrefab, m_podLocation, Quaternion.identity);

not sure if this is what you want

Just put a script on the prefab to hold a reference to the object that created it.

public class Holder {
    public GameObject creator;

obj = (GameObject)Instantiate (PodPrefab, m_podLocation, Quaternion.identity);
obj.GetComponent<Holder>().creator = this.gameObject;

Btw. To be nitpicky, Object.Instantiate is a static method so you can’t really say “the gameObject that created it”.