• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by emotitude · Jan 30, 2012 at 08:52 AM · collisiongameobjectplayerhealth

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;
         }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image senad · Jan 30, 2012 at 09:06 AM 0
Share

What exactly does not work? Do the collision fuctions get entered? Do the if-statements get entered?

avatar image emotitude · Jan 30, 2012 at 09:23 AM 0
Share

The game objects doesn't affect in any way on the health of the player on collisions.

avatar image senad · Jan 30, 2012 at 09:44 AM 0
Share

The more precise you are, the easier it is to find a solution. :)

Try to do some debugging and answer my two questions above.

avatar image emotitude · Jan 30, 2012 at 11:13 AM 0
Share

The collision functions and if statements are not getting entered!! :|

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
        }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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; } }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image emotitude · Jan 30, 2012 at 02:40 PM 0
Share

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.

avatar image fafase · Jan 30, 2012 at 03:09 PM 0
Share

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.

avatar image emotitude · Jan 30, 2012 at 04:49 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers

script help gameobject to playercar 1 Answer

Enemy not taking damage on collisions. 2 Answers

Character Object Collision lowers health, knocks player backwards 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges