Making a time bomb
I want to spawn multiple bombs in random locations, and give them 5 seconds before detonation. Yet, all the clones switch their tags back to "unt" without changing to "explosion" after 5 seconds, except the first original bomb.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class sc : MonoBehaviour {
public Sprite bomb;
public Sprite explo;
public GameObject player;
public GameObject bx;
public float timer;
// Start is called before the first frame update
void Start ()
{
// this.gameObject.GetComponent<SpriteRenderer>().sprite = bomb;
timer = 0;
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer >= 5)
{
if (this.gameObject.GetComponent<SpriteRenderer>().sprite == bomb)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = explo;
this.gameObject.tag = "explosion";
}
if (timer >= 6)
{
bx.transform.position = new Vector3(10, 0, 0);
this.gameObject.tag = "unt";
timer = 0;
return;
}
}
}
private void OnTriggerStay2D(Collider2D collision)
{
Debug.Log("trigger");
if(bx.tag == "explosion")
{
Destroy(player);
}
}
}
And here is the code for the spawner:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spwnrrr : MonoBehaviour {
public GameObject bombPrefab;
public float timer;
public float posx;
public float posy;
// Start is called before the first frame update
void Start()
{
timer = 0;
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer >= 5)
{
transform.position = new Vector3(Random.Range(-4, 4), Random.Range(-4, 4), 0);
posx = transform.position.x;
posy = transform.position.y;
Instantiate(bombPrefab, transform.position = new Vector3(posx, posy, 0), Quaternion.identity);
timer = 0;
}
}
}
Answer by harshdhillon56 · Mar 11, 2021 at 06:49 AM
instead of changing the sprite destroy or SetActive(fasle); and instantiate any kind of particle effect
Your answer
Follow this Question
Related Questions
Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers
How to instantiate a prefab using a string variable ? 1 Answer
null reference on instantiated prefab array at instantiation 1 Answer
instantiate same object multiple time at different positions unity2d 0 Answers
Problems with instantiated objects in a circle formation 0 Answers