- Home /
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!
does you're boss health go down when you hit it with a fireball?(check the inspector)
@A$$anonymous$$J9 : 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.
I cannot see this variable updating. What would I have to do in the inspector to find this?
Try attaching a separate script to the boss itself. Like this:
using UnityEngine;
using System.Collections;
public class BossScript : $$anonymous$$onoBehaviour {
//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.
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.
Your answer
Follow this Question
Related Questions
Error: Instantiated Enemies don't get hit 2 Answers
Destroy and Spawn an Enemy 1 Answer
Instantiate with Prefabs 2 Answers
Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer
Enemy not spawning correctly 1 Answer