Hello,
I kind of hit a snag. I’m sorry if this is an elementary level problem but I’m still learning the Unity engine. There may be a pre-existing code, but I’m trying to learn how to do things vs. how to apply what someone else has done until I get a good handle on everything. Here is what I have in terms of set up…
Scene/Setup: A tank fires its cannon at an object which is a prefab of either a cube or a sphere. The cube and sphere prefabs have the same general script named “Enemy.js.” I have two spheres and two cubes in the scene for the tank to shoot at.
Problem: Only one of the four objects ever takes damage and gets destroyed - one of the two cubes.
Theory: I am assuming it is because the Enemy.js script is on both prefabs, but if that were really the problem, why is only one cube being destroyed instead of both cubes regardless of what I’m shooting at? I thought it had something to do with the object ID, but I have done debugging by printing the object instance IDs and I have confirmed different instance IDs.
Code:
Enemy.js
static var INCOMING_DAMAGE = 0;
private var enemyHp ;
var enemySphereMaxHP = 5;
var enemyCubeMaxHP = 3;
var currentHp = 0;
function Start ()
{
var enemyType = gameObject.name;
switch (enemyType)
{
case "Enemy_Cube" :
enemyHp = enemyCubeMaxHP;
//currentHp = enemyHp;
break;
case "Enemy_Sphere" :
enemyHp = enemySphereMaxHP;
//currentHp = enemyHp;
break;
}
}
function Update ()
{
if (INCOMING_DAMAGE != 0)
{
enemyHp -= INCOMING_DAMAGE;
//currentHp = enemyHp;
//print (gameObject.name + " HP = " + enemyHp);
if (enemyHp <= 0)
{
Destroy(gameObject);
}
INCOMING_DAMAGE = 0;
}
}
Turret_Shell.js
var Splodie : Transform; // the explosion object, which has been assigned in the editor
var Dur = 10.0;
private var LoopCounter;
var turretShellDamage = 2;
function Awake ()
{
Destroy(gameObject, 2.0);
}
function OnCollisionEnter()
{
var Explosion = Instantiate(Splodie, transform.position, transform.rotation);
Destroy(gameObject);
Destroy(Explosion.gameObject, 1.0);
Enemy.INCOMING_DAMAGE = turretShellDamage;
}
Any thoughts on what I’m doing wrong?
Thanks,
-S