• 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Moving Platform Collisions with Custom Movement Script 0 Answers

How to emit a sound on specific collision. 1 Answer

Damage script is screwed up...? what to do? 1 Answer

How to Destroy a gameobject on collision 3 Answers

Do a collider only with certain taged objects 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges