Search Unity

Replace Instantiated Object

Discussion in 'Scripting' started by andymuir77, Nov 20, 2017.

  1. andymuir77

    andymuir77

    Joined:
    Nov 13, 2017
    Posts:
    9
    I have Instantiated 100 random objects at random locations. I want to replace each one with a smashed version on collision. I can do this with a single item on scene with with:

    public GameObject myObject;

    void OnCollisionEnter()
    {
    Instantiate(myObject, new Vector3(transform.position.x,4.3f, transform.position.z), transform.rotation);
    Destroy(gameObject);
    }

    and adding the object to replace in the inspector. However this doesn't seem to work with Instantiated Objects as it the object to replace is not defined.

    I thought I could reference the object by script with something like:
    myObject = GameObject.Find("/Objects/myObject");

    But everything I try returns null.

    Can anyone tell me how to replace without declaring and defining a public GameObject?
     
  2. andymuir77

    andymuir77

    Joined:
    Nov 13, 2017
    Posts:
    9
    Just figured out my error. I was assigning the smashed object to a version in the scene and hitting apply, which doesnt work. All I had to do was assign the smashed object in the prefabs folder.