Question: Unity2D c# How to destroy GameObject & Instantiate new one after a while? I want to resurrect the tank after being destroyed by bullets and

Unity2D c#

How to destroy GameObject & Instantiate new one after a while?

I want to resurrect the tank after being destroyed by bullets and restore its original attributes. But the following code will make the tank lose all its attributes after resurrection.How can I modify it to meet the conditions and allow the tank to resurrect indefinitely?

public class Bullet : MonoBehaviour {

GameObject tank1; GameObject tank2; public int bullet; bool isDestroy;

// Start is called before the first frame update void Awake() { tank1 = GameObject.Find("Tank1"); tank2 = GameObject.Find("Tank2"); }

void OnTriggerEnter2D(Collider2D other) { if (bullet == 1) { if (other.tag == "Wall") { Destroy(this.gameObject); }

if (other.tag == "Tank2") { Destroy(this.gameObject); Destroy(other.gameObject); tank2= Instantiate(tank2, transform.position, transform.rotation); } }

else if (bullet == 2) { if (other.tag == "Wall") { Destroy(this.gameObject); }

if (other.tag == "Tank1") { Destroy(this.gameObject); Destroy(other.gameObject); tank1 = Instantiate(tank1, transform.position, transform.rotation); } }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!