UNITY SCRIPTS:spawn in same location after getting destroyed(after 3 seconds)

unity spawn object after destroy
unity create object after destroy
unity destroy instantiated object
unity respawn object

hey does anyone knows how to make a C# script for object to re spawn after getting destroyed by a bullet


There are multible ways to archive this.

Note that you can also work completely without Coroutine but this usually sucks

private float timer;

private bool isRespawning;
private GameObject clone;

private void DestroyObject(GameObject obj)
{
    isRespawning = true;
    timer = 3f;

    // first make a clone of the current Object
    var clone = Instantiate(obj, obj.transform.position, obj.transform.rotation, objtransform.parent);

    // disable the clone
    clone.SetActive(false);

    // Destroy the original
    Destroy(obj);
}

private void Update()
{
    if(Mathf.Approximately(timer,0f)) return;

    timer += Time.deltaTime;

    if(timer > 0) return;

    // Set the clone active
    clone.SetActive(true);
    timer = 0;
}

Simplier would be a Coroutine:

private void DestroyAndRespawn(GameObject obj)
{
    StartCoroutine(DestroyAndRespawnRoutine(obj));
}

private IEnumerator DestroyAndRespawnRoutine(GameObject obj)
{
    // first make a clone of the current Object
    var clone = Instantiate(obj, obj.transform.position, obj.transform.rotation, objtransform.parent);

    // disable the clone
    clone.SetActive(false);

    // Destroy the original
    Destroy(obj);

    // Wait 3 seconds
    yield return new WaitForSeconds(3f);

    // Set the clone active
    clone.SetActive(true);
}

But why should you destroy the object and respawn it? You can simply only disable and later re enable it

private void DestroyAndRespawn(GameObject obj)
{
    StartCoroutine(DestroyAndRespawnRoutine(obj));
}

private IEnumerator DestroyAndRespawnRoutine(GameObject obj)
{
    // disable the obj
    obj.SetActive(false);

    // Wait 3 seconds
    yield return new WaitForSeconds(3f);

    // Set the objactive
    obj.SetActive(true);
}

Update

Since you want to call this on collision:

You will need another GameObject that works as a Manager for the respawning / enabling, disabling since you can not just attach that script to the object you disable -> would also disable the script and never re-enable the object.

  1. Create a new empty GameObject e.g. called "RespawnManager"
  2. Create a new Script RespawnManager.cs:

    public class RespawnManager : MonoBehaviour
    {
        public static RespawnManager Singleton;
    
        private void Awake()
        {
            if(Singleton)
            {
                enabled = false;
                return;
            }
    
            Singleton = this;
        }
    
        private void DestroyAndRespawn(GameObject obj)
        {
            StartCoroutine(DestroyAndRespawnRoutine(obj));
        }
    
        private IEnumerator DestroyAndRespawnRoutine(GameObject obj)
        {
            // disable the obj
            obj.SetActive(false);
    
            // Wait 3 seconds
            yield return new WaitForSeconds(3f);
    
            // Set the objactive
            obj.SetActive(true);
        }
    }
    
  3. Than you can call it on collision like

    OnCollisionEnter(Collision obj)
    {
        if (obj.gameObject.name != "bullet") return;
    
        // pass this GameObject to the manager
        RespawnManager.Singleton.DestroyAndRespawn(gameObject);
    }
    

The Destroy function can take a parameter saying how long it will be before the object is destroyed. Also that script would be made a lot simpler  The spawner that i am trying to make works like this, there is a game object at a set location for example vector3(-1,0.25,0) and when i click that game object i would like it to be destroyed and after a set ammount of seconds for example 0.5f and spawn at the same location another random game object .


Set object active to False. using

gameobject.SetActive(false);

And you can use Coroutine to do this.

I've done one where the item is on a timer I'm having trouble getting items to respawn when it isn't right away (3 seconds later instead). send message to spawn new item with location wait time won't continue to run, so it's never going to spawn a new object (the script to do so has been destroyed). // JavaScript var wreck : GameObject; // As an example, we turn the game object into a wreck after 3 seconds automatically function Start { yield WaitForSeconds(3); KillSelf(); } // Calls the fire method when holding down ctrl or mouse function KillSelf { // Instantiate the wreck game object at the same position we are at var wreckClone


If you use Destroy(yourObject);, the object does get removed and you loose access. You could make your Object into a prefab and spawn it where you want by using GameObject obj = Instantiate(...); Obj contains the reference to the new spawned object and not to the prefab, which means you can later again use Destroy(obj);

I have a game object with a script attached called "playerController". with "​playerController" attached and re-spawn it after 3 seconds. And if you just use Instantiate it will spawn the object with the given position/rotation  I am creating a game, with-in the game there's 7 GameObject Prefabs, each one Instantiate it's on level, I am now creating the main script that will instantiate each GameObject Prefab, now what I want to do, is so create each 80f "seconds" to instantiate a new GameObject Prefab, and destroy the last one, for example, in 0f seconds since Level


From Concept to Playable Game with Unity and C# Jeremy Gibson Bond. Now, when you play the scene, you should see that the Enemy_0 ship moves down the then off screen, and within a couple of seconds of moving off screen, it is destroyed. Spawning Enemies at Random With all of this in place, it's now possible to  Once in the spawning system, state updates are sent to clients when the object changes on the server, and the object will be destroyed on clients when it is destroyed on the server. Spawned objects are also added to the set of networked objects that the server is managing, so that if another client joins the game later, the objects will also be


I am trying to build a simple spawn mechanic that will spawn objects and send here is my spawn script any help would be much appreciated. mesh.enabled = true;; //Destroy after x seconds; Destroy (clone, 3);; if(clone. The object obj is destroyed immediately after the current Update loop, or t seconds from now if a time is specified. If obj is a Component, this method removes the component from the GameObject and destroys it. If obj is a GameObject, it destroys the GameObject, all its components and all transform children of the GameObject . Actual object


I am developing a Bullet Hell shooter that uses Object Pooling to spawn bullet prefabs that destroy themselves after a set number of seconds  If Unity doesn't want us to destroy it immediately it should warn us or throw an exception directly. I checked my code so may times and was upset to found that there was nothing else wrong, I just didn't know it won't be destroyed like that