Hi! This was my first attempt making a sinple health/enemy script where everytime the player clicks the left mouse button, the cube loses health and then disappear when health reaches zero.
Everything works fine, except for I got some trouble adding this:
- The cube respawns after 3 seconds (I tried using yield WaitForSeconds(3);, but with no luck)
- Everytime the cube "dies", the variable timesDead goes one up and when the cube has died 3 times, the message appears "You've killed all the enemies!". Theres only the problem that seems to after you've killed the cube and a new one disappear, the variable sets to the default zero again, except for the +1 that got addet when it died. For some reason it won't remember that the variable got updatet. Im pretty sure there is a way to store this, I just need a helping hand! :)
It's my very first script i've wrote totally by myself. You guys may think thats its nothing, but it's a big step for me! The code might be a little messy, but here we go:
#pragma strict
var health : int = 100; // The health
var damage : int = 50; // The damage the player deals when he left clicks.
var timesDead : int = 0;
function Update()
{
if(Input.GetButtonDown("Fire1")) // Checks if player use the left mb
{
DamageDone();
print("You damaged the enemy!"); // Prints to console that you damaged the enemy
if(health <= 0) // if health is 0 or below then
{
print("You killed the enemy!"); // Print the enemy get killed
timesDead++;
Dead();
Wait();
if(timesDead >= 3) // If the enemy is dead 3 times or more, print
{
print("No more enemies to kill. You won!");
}
} }
}
function Dead()
{
Destroy (gameObject);
}
function DamageDone() // the damage function
{
health -= damage;
}
function Wait()
{
yield WaitForSeconds(1.50);
print(Time.time);
var enemy = GameObject.CreatePrimitive(PrimitiveType.Cube);
enemy.AddComponent(enemyScript);
}
↧