Jump to content

Hi LTT!

 

I'm seeking help here, because on the Unity forums I didn't get any help whatsoever. What I'm trying to do is to make some object (in this case bombs) reappear after they 'explode' in a certain period of time. Can someone either tell he how or give me a snippet of a code? I didn't use renderer.enabled = true or false because it still interacts with the game world.

 

Here's how i destroyed the object:

public class Detonate : MonoBehaviour {
 
    public float destroyTime = 3;
 
    void Update () {
        Destroy(gameObject, destroyTime);
    }
}

 

Link to comment
https://linustechtips.com/topic/577323-unity-c-respawn-script-help/
Share on other sites

Link to post
Share on other sites

Can't you look into a timer (yield) like function

First you make it check if a bool is on true or false, if true: you can spawn in the bomb (I would recommend looking into the initiate function), use the yield function to wait some time, explode the bomb, wait a couple of seconds, destroy the gameobject and spawn a new bomb?

 

If you give me a bit more information on what you are trying to achieve, I can help you further, as I now dont fully understand  what you are trying to achieve. Will go offline now though, so be sure to quote me so I get a notification and can help you tomorrow.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

32 minutes ago, Minibois said:

Can't you look into a timer (yield) like function

First you make it check if a bool is on true or false, if true: you can spawn in the bomb (I would recommend looking into the initiate function), use the yield function to wait some time, explode the bomb, wait a couple of seconds, destroy the gameobject and spawn a new bomb?

 

If you give me a bit more information on what you are trying to achieve, I can help you further, as I now dont fully understand  what you are trying to achieve. Will go offline now though, so be sure to quote me so I get a notification and can help you tomorrow.

Pretty much the following: I made an object to rest on the surface (plane), and I made 50 spawn points on the surface where these object spawn when i run the 'game' (each bombs (8 of them) spawns in one of the 50 spawn points at random). after 2-3 seconds the 'blow up', and i use the Destroy script i quoted on how i destroy them. Now i just want to make them respawn and respawn and respawn until i die (dying isn't important but if you want to know it will be some kind of unit collision between the player and the 'bomb'). Hope i clarified what i want to do :D

Link to post
Share on other sites

8 hours ago, Zeinone said:

Pretty much the following: I made an object to rest on the surface (plane), and I made 50 spawn points on the surface where these object spawn when i run the 'game' (each bombs (8 of them) spawns in one of the 50 spawn points at random). after 2-3 seconds the 'blow up', and i use the Destroy script i quoted on how i destroy them. Now i just want to make them respawn and respawn and respawn until i die (dying isn't important but if you want to know it will be some kind of unit collision between the player and the 'bomb'). Hope i clarified what i want to do :D

Ah, so it's like a game where you have to run around so you dont get hit by the respawning bombs? I get it.

 

It's probably best to do the spawning and detonating+destroying in a different script from each other.

The spawning script I would put on something like the main camera, an object that is just in the scene and doesn't do anything.

Assuming the bomb object you use are prefabs: put the detonating and destroying script on the bomb prefab (you can do that by going to the prefab in the Unity file explorer and do add component).

 

The spawning script will contain the spawning script only contains the spawning code and nothing more. The detonating script will contain the exploding and the destroy object script.

In the spawning script I would make it check if there are more than 8 bombs in the field already, if less than that you spawn more bombs. The detonating script I would probably put in the update a wait/yield command so it doesn't explode on spawning in, after that yield I would make it explode, yield for maybe 2 more sec (or however long the axploding animation is) and after that destroy th gameobject

 

 

 

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

It would better to implement an object pooling technique than to destroy/spawn a pretty much identical game object so many times, which can be costly during runtime. Read into it.

It would be kinda like :

public class BombDestroy : MonoBehaviour
{
	public float destroyTime = 2f;
	void onEnable()
	{
		Invoke("Destroy",destroyTime);
	}
	void Destroy()
	{
		gameObject.SetActive(false);
	}
	void onDisable()
	{
		CancelInvoke();
	}
}
public class BombSpawner : MonoBehaviour
{
	public int pooled = 8;
	List<GameObject> pool;
    	public GameObject bomb;
  	public float spawnTime = 3f;
  	void Start()
    	{
      	pool = new List<GameObject>;
      	for(int i = 0; i < pooled; ++i)
        {
            GameObject o = (GameObject)Instantiate(bomb);
            o.setActive(false);
            pool.Add(o);
        }
      	InvokeRepeating("Spawn", spawnTime, spawnTime);
    	}
  
  	void Spawn()
    	{
      		for(int i = 0; i < pool.Count; ++i)
          	if(!pool[i].activeInHierarchy)
        	{
          		pool[i].transform.position = <some spawn point position>;
          		pool[i].transform.rotation = <some rotation>;
          		pool[i].setActive(true);
          		break;
        	}
    	}
  
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×