Instantiate /destroy , problem problem problem :p

hello !
hello, i just want to destroy an object after collision with player, then instantiate an object in the position of the original. But doenst Work !!!
( i want to make a lot of this but if i clone…fail )

this is for Create the object. I put this on an empty , child of the object that i want to destroy.

This code is like a manager to create, check.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreadorObjetos : MonoBehaviour {
    GameObject original;
    public GameObject roto;
    GameObject clonado;
    bool checkRoto = false;
    static public bool checkDestroyed = false;
    void Start()
    {
        Creador();
       
    }


   public  void Creador()
    {

     original= GameObject.CreatePrimitive(PrimitiveType.Cube);
     original.transform.position = transform.position;
     
     original.transform.localScale = new Vector3(2, 2, 2);
    }

   public void CreadorRoto()
   {
       if (checkRoto == false)
       {
           clonado = Instantiate(roto, original.transform.position, original.transform.rotation) as GameObject;
       }
   }
    void Update ()
    {
        Destroyer();
    }

   public  void Destroyer()
    {

        if (checkDestroyed == true)
        {
            Debug.Log("Destroyer funciona");
            Destroy(original, 0.1f);
        }

        if (DetectCollision.hayColision == true)
        {
            checkRoto = true;
            Debug.Log("hay colision es true");
            Destroy(clonado, 2f);

        }


       
    }
}

and this for detect the Colision

   public BoxCollider thisBox;
    public static bool hayColision = false;

   public void OnCollisionEnter(Collision other)
    {
        if (other.collider.tag == "Player")
        {
            FindObjectOfType<CreadorObjetos>().CreadorRoto();
            CreadorObjetos.checkDestroyed = true;
            hayColision = true;
            thisBox.isTrigger = true;
        }

    }

if i put 2 objects for destroy , the first is instantiated on the position of the second…

Thanks for Try it !

The concept sounds simple. Instead of trying to use FindObjectOfType, you’d probably be better off using GetComponent (or GetComponentInChildren, if you want to keep it on a child for some reason), and use that reference to begin your cloning & positioning. I have a feeling that’ll work for ya.

thz, i could fix, i dont remember how, but i do it ! : )