0
\$\begingroup\$

After destroying a gameobject in unity, how to instantiate it and make it do the exact same thing what the gameobject before getting destroyed was doing? My gameobject is a car which follows a path made out of nodes. After it is destroyed at a particular node, I want to instantiate it at a particular point which is the spawnSpot in my code, then I want it to follow the same path again and then get destroyed again and get instantiated again and follow the same path again and so on. Right now, I am able to make my car follow a certain path and then destroy it at a certain node and and instantiate it and make it follow the same path again. Then my car is destroyed again and instantiated again but this time it does not follow the path and stays right at the spawnPoint. I am using the destroy and instantiate functions in fixed update. Here's the relevant part of my code:

private void Destroy()
{
    if(currentNode == nodes.Count - 1)
    {
        Destroy(this.gameObject);
    }
}

private void Instantiate()
{
    if(currentNode == nodes.Count - 1)
    {
        Instantiate(prefab, spawnspot, Quaternion.identity);
    }
}

P.S. prefab is the gameObject.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "make it do the exact same thing what the gameobject before getting destroyed was doing" - do you mean it's using the same behaviour logic independently, or that it's literally recording a set of actions and re-playing them? At present it's not clear what flaw/gap in your current code you're trying to fix, or what kind of help you need with this feature. Please consider editing your question to include more explanation, and show where your Destroy, Instantiate, and nodes are used. \$\endgroup\$
    – DMGregory
    Jun 8 '17 at 15:54
2
\$\begingroup\$

Instead of destroying the object, just deactivate it. And inactive game object is no longer doing anything, but can be reactivated at any time. It will retain all of its state.

\$\endgroup\$
0
0
\$\begingroup\$

You can do this simply by copying your gameobject in another gameobject and then use that gameobject instead of prefab.

GameObject copiedGameObject;

private void Destroy()
{
    if(currentNode == nodes.Count - 1)
    {
        copiedGameObject = this.gameObject;
        Destroy(this.gameObject);
    }
}

private void Instantiate()
{
    if(currentNode == nodes.Count - 1)
    {
        // No need to give position and rotation becasue it automatically take last stored rotation and position.
        Instantiate(copiedGameObject);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ are you able to solve your problem with my solution? @D.g \$\endgroup\$ Jun 10 '17 at 4:40
  • \$\begingroup\$ Actually I worked on this for long so I could fix it before reading your answer but thanks for taking time out to reply. \$\endgroup\$
    – D.g
    Jun 11 '17 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.