• 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
Question by AKJ9 · Mar 30, 2014 at 05:55 PM · instantiateprefabdestroyenemyspawn

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'?

Hi all,

I only have basic ability with Unity and cannot figure out the answer to what looks like a simple question. I have three enemies in my level, and would like to spawn a 'boss' once they're defeated. Here is my code so far:

 public static var enemyCount : int = 3;
 public static var bossHealth: int=3;
 
 function OnCollisionEnter( collision : Collision ) 
 {
    if(collision.gameObject.tag == "fireball")
    {
       animation.Play("dying");
       audio.Play();
       yield WaitForSeconds(0.2);
       GameObject.Destroy ( gameObject ) ;
       enemyCount -= 1;
    }

    if(enemyCount <= 0)
    {
       var instance : GameObject = Instantiate(Resources.Load("boss"));

       if(collision.gameObject.tag == "fireball")
       {
          animation.Play("dying");
          audio.Play();
          bossHealth -= 1; 

         if(bossHealth<=0)
         {
            yield WaitForSeconds(0.2);
            GameObject.Destroy ( gameObject ) ;   
            Application.LoadLevel ("Level_1_a_Clear");
         }
       }
    }
 }

This script is attached to every enemy in the game. In fact, at the moment, all enemies are the same (loaded from a prefab). Currently, what happens is that when I defeat all three enemies, my boss spawns. Great! Problem is, he cannot be killed. I can shoot infinite fireballs at him and he plays his 'hit' animation and audio clip but continues to live! Do any of you see where I'm going wrong here?

Your help is much appreciated.

Many thanks!

Comment

People who like this

0 Show 8
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
avatar image FirePlantGames · Mar 30, 2014 at 05:59 PM 0
Share

does you're boss health go down when you hit it with a fireball?(check the inspector)

avatar image Lo0NuhtiK · Mar 30, 2014 at 06:00 PM 0
Share

@AKJ9 : when posting questions/answers/comments with code in them please be sure to use the 101/010 button and format the code properly. I edited your post for you this time.

avatar image AKJ9 · Mar 30, 2014 at 06:02 PM 0
Share

I cannot see this variable updating. What would I have to do in the inspector to find this?

avatar image Nightdr · Mar 31, 2014 at 02:03 AM 0
Share

Try attaching a separate script to the boss itself. Like this:

 using UnityEngine;
 using System.Collections;
 
 public class BossScript : MonoBehaviour {
     //if variable isn't static then it shows in inspector
     public int bossHealth = 3;
     void OnCollisionEnter(Collision collision){
         if(collision.gameObject.tag == "fireball"){
             //this log makes sure that he is getting hit by fireball
             Debug.Log("I was hit by a fireball");
             animation.Play("dying");
             audio.Play();
             // take away health
             bossHealth--; 
             
             //check to see if boss is dead
             if(bossHealth<=0){
                 // JS WaitForSeconds throws error in CS
                 //yield WaitForSeconds(0.2);
                 Destroy(gameObject);   
                 Application.LoadLevel ("Level_1_a_Clear");
             }
         }
     }
 }
 

Sorry for CS I don't know UnityScript.

avatar image getyour411 · Mar 31, 2014 at 02:18 AM 0
Share

To OP: when enemy count

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by normand · Sep 06, 2014 at 09:02 AM

its look like you have put the enemyhealt and the boss health togeter. You should split your script for one of each.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Enemy not spawning correctly 1 Answer

i need to destroy prefab clones 1 Answer

Error: Instantiated Enemies don't get hit 2 Answers

Destroy and Spawn an Enemy 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