- Home /
I want to respawn a Boss prefab when its destroyed, but it isnt working.
Spawner script. using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine;
public class BossSpawner : MonoBehaviour {
//to restrict the window of spawming
public float minY = -4.3f;
public float maxY = 4.3f;
public float maxX = 6.84f;
//to store enemy ship
public GameObject bossPrefab;
public float spawnDelay = 150f;
public float spawnDelay1 = 155f;
public float currentDelay;
public float randomDelay;
public bool isDestroyed = false;
public int curent;
public int totalSpawns = 16;
public int currentSpawns = 0;
public int count;
// Start is called before the first frame update
public void Start()
{
currentSpawns += count;
randomDelay = Random.Range(spawnDelay, spawnDelay1);
currentDelay = randomDelay;
Invoke("spawner", currentDelay);
Invoke("cancelSpawner", 1);
}
public void Update()
{
Invoke("cancelSpawner", 1f);
Invoke("stopSpawning", 3f);
}
public void spawner()
{
if (currentDelay >= randomDelay)
{
Vector3 temp = transform.position;
Instantiate(bossPrefab, temp, Quaternion.Euler(0f, 0f, 90f));
}
Invoke("spawner", currentDelay);
count++;
currentDelay += Time.deltaTime;
/*
if (Boss.isDestroyed == true)
{
Vector3 temp = transform.position;
Instantiate(bossPrefab, temp, Quaternion.Euler(0f, 0f, 90f));
Invoke("spawner", 1);
Boss.isDestroyed = false;
}
*/
}
public void cancelSpawner()
{
if(Boss.isDestroyed == true)
{
Vector3 temp = transform.position;
Instantiate(bossPrefab, temp, Quaternion.Euler(0f, 0f, 90f));
Invoke("cancelSpawner", 1);
Invoke("spawner", 1);
Boss.isDestroyed = false;
}
Invoke("cancelSpawner", 1);
Boss.isDestroyed = false;
}
public void stopSpawning()
{
if(currentSpawns >= totalSpawns)
{
//stop game and play a cutscene
CancelInvoke("spawner");
Debug.Log("You Won");
}
}
}
Boss Script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Boss : MonoBehaviour { public float speed = 3f; public bool canShoot = false; public bool canMove = true; public Transform attackPoitn1; public Transform attackPoint2; public GameObject bossBullet; private Animator anim;
//to keep health
public int maxHealth = 50;
public int currentHealth;
//to spawn boss after a certain score
public int maxScore = 40;
public int currentScore;
public static bool isDestroyed = false;
public float maxX = 6.84f;
void Awake()
{
//to get audio source and animator
anim = GetComponent<Animator>();
}
// Start is called before the first frame update
void Start()
{
/*
GameObject theBoss = GameObject.FindWithTag("Boss1");
BossSpawner bsSpnr = theBoss.GetComponent<BossSpawner>();
isDestroy = bsSpnr.isDestroyed;
*/
currentScore = maxScore;
currentHealth = maxHealth;
Rigidbody2D rb2d = GetComponent<Rigidbody2D>();
if (canShoot)
Invoke("bossShoot", Random.Range(0f, 0.5f));
}//start
// Update is called once per frame
void Update()
{
moveBoss();
}//update
void moveBoss()
{
if (canMove)
{
Vector3 temp = transform.position;
temp.x -= speed * Time.deltaTime;
transform.position = temp;
if (temp.x < maxX)
temp.x = maxX;
transform.position = temp;
}
}//moveBoss
void bossShoot()
{
GameObject bullet = Instantiate(bossBullet, attackPoitn1.position, Quaternion.identity);
bullet.GetComponent<BossBullet>().isBossBullet = true;
GameObject bullet1 = Instantiate(bossBullet, attackPoint2.position, Quaternion.identity);
bullet1.GetComponent<BossBullet>().isBossBullet = true;
if (canShoot)
Invoke("bossShoot", Random.Range(0f, 1f));
SoundManager.PlaySound("BossShoot1");
//to play bullet sound
}//bossShoot
void turnOffGameObject()
{
gameObject.SetActive(false);
}
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Bullet"|| target.tag == "Ship")
{
takeDamage(10);
}
}//trigger
public void takeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0)
{
isDestroyed = true;
if (isDestroyed == true)
{
canMove = false;
if (canShoot)
{
canShoot = false;
CancelInvoke("bossShoot");
}
SoundManager.PlaySound("bossExp");
anim.Play("destroy");
Destroy(gameObject);
isDestroyed = false;
//to play destroy animation and destroy sound
}
}
//isDestroyed = false;
}
}//class
Answer by HappyPixel27 · Jul 03, 2020 at 01:28 AM
public GameObject Boss;
public Vector3 blahPos;
public Quaternion blahRotation;
function NewBoss () {
if (BossDead == true) {
Instantiate (Boss, blahPos, blahRotation);
BossDead = false;
}
}
Not sure if this is what you wanted but try this
Your answer
![](https://koobas.hobune.stream/wayback/20220613002332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving an object forward, killing the object and respawning a new one. 1 Answer
multiplayer die and respawn 0 Answers
GUI.Button Not Working Correctly Strange 1 Answer
Re-spawning issue 1 Answer