Hi
I created a prefab named block1 and added a script on them named sheepController which does following:
var startingHealth:int = 2;
var currentHealth:int;
var isDead:boolean;
function Awake () {
currentHealth = startingHealth;
}
public function HitSheep (sheep:GameObject) {
Debug.Log("Hit Sheep startet");
Debug.Log("Current Health: " + currentHealth);
if (isDead)
{
Debug.Log("Sheep is dead");
return;
}
//currentHealth = currentHealth - 1;
currentHealth -= 1;
Debug.Log("Current Health: " + currentHealth);
if (currentHealth <= 0)
{
Debug.Log("Current Health <= 0");
Destroy(sheep);
Debug.Log("Destroy Sheep");
isDead = true;
}
}
After that I instantiate the prefab (block1) in another script:
function createBlocks()
{
for(var j:int = -5; j<6; j++)
{
Instantiate(block1, Vector3(j*21, 20, 0), Quaternion.identity);
}
}
On Collision I like to subtract health of block1 by 1 each hit until it reached <= 0 .Then the block should be destroyed.
function OnCollisionEnter(col:Collision)
{
if (col.gameObject.tag == "block1")
{
Debug.Log("Hit Block1");
audio.PlayOneShot(bricksound,0.2);
var scontroller:sheepController;
scontroller = GameObject.FindGameObjectWithTag("block1").GetComponent(sheepController);
if (col.gameObject != null)
{
scontroller.HitSheep(col.gameObject);
} else {
Debug.Log("col.gameObject is null!");
}
if (gameController.score >= 0)
{
gameController.score = gameController.score + 10;
}
}
}
The problem now the prefab count is not set for each prefab. It means if count is less then 0 on prefab hit it will be destroyed. I like to have each prefab has a start by 2 and subtracting by 1. Means two hits until destroy.
What I’m doing wrong?
Thanks for help…