- Home /
Two gameObjects on collision one increase the health and the other decrease the health of the player.
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;
}
}
What exactly does not work? Do the collision fuctions get entered? Do the if-statements get entered?
The game objects doesn't affect in any way on the health of the player on collisions.
The more precise you are, the easier it is to find a solution. :)
Try to do some debugging and answer my two questions above.
The collision functions and if statements are not getting entered!! :|
Answer by emotitude · Jan 30, 2012 at 05:20 PM
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);
}
}
Answer by fafase · Jan 30, 2012 at 12:23 PM
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; } }
I get this error on running. 'player' already has a definition for 'OnCollisionEnter(UnityEngine.Collision)'.
So I changed it to this.
var Health = 100;
function Update () { Debug.Log(Health); }
function OnCollisionEnter(other : Collision ) { if (other.gameObject.Tag("healthy")) { Health += 5; Destroy(other.gameObject); } else if (other.gameObject.Tag("enemy")) { Health -= 2; } }
the error stopped but it is still not affecting the health of the player.
Your error could be that you use "Player" tag as the other object you are colliding with, when you are already in the player object (you follow me?). fct OnCollisionEnter (other : Collision) means that when the object the script is attached is entering collision with 'other' it returns a collision (...). In the if statement, you check if the other object has a tag name equal to what you want it to be. So logically, if you attached the script to the player and chek if it collides with other.gameObject.Tag =="Player"; it tells you it is already there.
Also, as you are starting with unity, did you set the tag properly? Tag is not name of the object. Tag is in the top of the inspector. You have name and tag.
If you use the script above attached to your player with the tag of the other objects colliding that should work.
Its working fine now, the error was that onCollisionEnter was added two times on the script, so I added both the enemy object and the health object inside one. and the other problem was i had to use CompareTag ins$$anonymous$$d of Tag, Which after changing worked fine. Thanks for the support @fafase. :D