Hey Guys, I have two game objects , one to increase the health and disappear on collision, and the other to decrease the health on collision. These are the scripts i have used for each objects but it doesn’t seem to work.
The script on the player game object.
var Health = 100;
function Update () {
print(Health);
}
The script on the health object,
function OnCollisionEnter(other : Collision ) {
if (other.gameobject.Comparetag("Player"))
{
var tempScript = other.gameObject.GetCompanent("PlayerHealth");
tempScript.Health += 5;
Destroy(gameObject);
}
}
The script on the enemy object,
function OnCollisionEnter(other : Collision ) {
if (other.gameobject.Comparetag("Player"))
{
var tempScript = other.gameObject.GetCompanent("PlayerHealth");
tempScript.Health -= 2;
}
}
Thanks guys for the support. Finally got it to work. This is the final script which is doing fine.
var health = 100;
function Update () {
print(health);
}
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.CompareTag("enemyobject"))
{
health -= 2;
}
if (other.gameObject.CompareTag("healthobject"))
{
health += 5;
Destroy(other.gameObject);
}
}
Ok you could try another way,
Put all that in the same health script assigned to the player.
Notice that gameObject is no more gameobject. (dunno if that is big deal though but in case) Also, comparetag is just tag now and print becomes a debug function appearing in your console and at the bottom.
var Health = 100;
function Update () {
Debug.Log(Health);
}
function OnCollisionEnter(other : Collision ) {
if (other.gameObject.Tag("Tag of health object"))
{
Health += 5;
Destroy(other.gameObject);
}
}
function OnCollisionEnter(other : Collision ) {
if (other.gameObject.Tag("Enemy tag"))
{
Health -= 2;
}
}