Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by k_dhruv · Jul 02, 2020 at 08:08 PM · respawning

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

UFPS How to stop crates from respawning 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges