Remove and respawn object few seconds after impact (c#)

havent been able to find anything so far

I have an object I want to remove a few seconds after impact and then have it repawn again

how do i go about doing that?

I already have the basic destory on delay script but that starts as soon as I start the game

public class DestroyedWithDelay : MonoBehaviour {
     void Start() {
         Destroy(gameObject, 6);
     }
}

If the object has a rigidbody and a collider, you can use the OnCollisionEnter method as such:

void OnCollisionEnter(Collision collision) {
    StartCoroutine(Respawn(6f, 10f));
}

IENumerator Respawn(float timeToDespawn, float timeToRespawn) {
    yield return new WaitForSeconds(timeToDespawn);
    gameObject.SetActive(false);
    yield return new WaitForSeconds(timeToRespawn);
    gameObject.SetActive(true);
}

Make sure you import the System.Collections namespace by adding this so the top of your script:

using System.Collections;

no that didn’t do anything

Do you have any way if detection collision in the first place?
Like I said, you need an object with a collider and a rigidbody for this method.

Yes i have both rigidbody and mesh collider

Mesh Colliders tend to be too complex to be calculated for collision.
Is it possible to make the mesh a convex?

If you don’t want it to be convex, you could add a box-collider and make it a trigger.
Now you can use OnTriggerEnter.

void OnTriggerEnter(Collider col) {
    StartCoroutine(Respawn(6f, 10f));
}

IENumerator Respawn(float timeToDespawn, float timeToRespawn) {
    yield return new WaitForSeconds(timeToDespawn);
    gameObject.SetActive(false);
    yield return new WaitForSeconds(timeToRespawn);
    gameObject.SetActive(true);
}

ok that worked when I use box collider but only when I go straight threw it and not smash through the object

I have a script on the object that allow me to smash threw it
cause im using this tutorial

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

public class Destructible : MonoBehaviour {

    public GameObject destroyedVersion;

    void OnCollisionEnter()
    {
        Instantiate (destroyedVersion, transform.position, transform.rotation);
        Destroy(gameObject);
}

}

Bump

It would be better to just spawn in an entire new cube, so we dont have to worry about ‘broken’ objects.
Create an empty GameObject and add the BoxSpawner onto it.
Everytime a box despawns, it’ll invoke the spawner and generate a new box.

using System;
using System.Collections;
using UnityEngine;

public class Box : MonoBehaviour {

    void OnTriggerEnter(Collider col) {
        StartCoroutine(Respawn(6f, 10f));
    }

    IEnumerator Respawn(float timeToDespawn, float timeToRespawn) {
        yield return new WaitForSeconds(timeToDespawn);
        BoxSpawner.Respawn(timeToRespawn);
        Destroy(gameObject);
    }

    private void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            StartCoroutine(Respawn(1f, 2f));
        }
    }
}
using System;
using System.Collections;
using UnityEngine;

public class BoxSpawner : MonoBehaviour {

    // Assign box prefab.
    [SerializeField]
    private GameObject box;

    // Access-point for Box script.
    private static BoxSpawner instance;

    // Spawn the box when the game Starts.
    void Start() {
        instance = this;
        StartCoroutine(SpawnBox(0f));
    }

    IEnumerator SpawnBox(float respawnTime) {
        yield return new WaitForSeconds(respawnTime);
        Instantiate(box, transform.position, transform.rotation);
    }

    public static void Respawn(float respawnTime) {
        instance.StartCoroutine(instance.SpawnBox(respawnTime));
    }
}

Im confused when you say add BoxSpawner im assuming you mean the script
also when I add the script what wall do u want me to add to Box the non cracked or the cracked one.

and when I create a script for Box it just gives me an error
An object reference is required to access a non-static member ’ BoxSpawner.Respawn(float)’

@Magiichan

My bad, I changed the script and tested it this time.
I’ve updated the scripts in me previous post.

Im still a little lost on the box spawner script do you want me to add wall no cracked or wall cracked to Box. cause I both but they just spawner another object straight away

Okay, so you need to delete the box, just keep the spawner with the box prefab that isnt cracked.
When you start the game, it will spawn the box. Once in gets destroyed it’ll respawn.

1 Like