Destroying object, adding new as children different.

I just have script with destroying yourself by clicking 1 and adding new object at same place. The problem is I want that “another one” make as child Player, but I can’t :P. There is script: (thanks for anyone help)

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

public class eat : MonoBehaviour
{
public GameObject efekt;

void Start()
{

}
void Update()
{
    if (Input.GetKeyDown("1")) {

        Destroy(this.gameObject);
        Instantiate(efekt, transform.position, transform.rotation);
    }

}

}

After you destroy this.GameObject, the Object (and the script that is attatched to it) do no longer exist. Therefore the code after “destroy” won’t be executed.
Child Objects of this.GameObject would also be destroyed.

Put the script onto the parent object, put your GameObject as a child of the parent. Then you can destroy and instantiate your childs =)

Hey, Slender2000.
I couldn’t quite understand your question, but here is my solution according to what I understood.
As far as I can understand, you want an effect to be instantiated where the object previously used to be as a child of some object.


using System.Collections; using System.Collections.Generic; using UnityEngine;
public class eat : MonoBehaviour { public GameObject efekt; public GameObject parentObject;

     void Update()
     {
         if (Input.GetKeyDown("1")) {
             Destroy(this.gameObject);
             Instantiate(efekt, transform.position, transform.rotation, parentObject);
         }
     }