• 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 Eltore · Dec 26, 2012 at 05:51 PM · collisioncolliderplayerenemyhealth

Lose health on collision

[EDIT]: Yes, the "Is Trigger" is checked.

When the player collides with an enemy, he doesn't lose health. But when he attacks the enemy, enemy loses. And they have almost the same scripts.

Enemy.cs

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
     public int currentHealth = 2;
     public int maxHealth = 2;
     
     void Start () {
         
     }
     
     void Update () {
         if(currentHealth > maxHealth) {
             currentHealth = maxHealth;
         }
         
         if(currentHealth <= 0) {
             currentHealth = 0;
             Die();
         }
     }
     
     void Die() {
         Destroy(gameObject);
     }
     
     void OnTriggerEnter(Collider col) {
         if (col.tag == "Player") {
             ScoreAndHealthSystem sh = (Player)ScoreAndHealthSystem.GetComponent("ScoreAndHealthSystem");
             sh.currentHealth--;
         }
     }
 }

The problem is probably at "void OnTriggerEnter"


ScoreAndHealthSystem.cs

 using UnityEngine;
 using System.Collections;
 
 public class ScoreAndHealthSystem: MonoBehaviour {
     public int currentScore = 0;
     public int maxScore = 100;
     public int currentHealth = 3;
     public int maxHealth = 99;
     public int damage = 1;
 
     void Start () {
     
     }
     
     void Update () {
         if(currentScore > maxScore) {
             currentScore = maxScore;
         }
         
         if(currentScore < 0 ) {
             currentScore = 0;
         }
         
         if(currentScore == maxScore) {
             GiveExtraLife();
         }
         
         if(currentHealth < 0) {
             currentHealth = 0;
             GameOver();
         }
         
         if(currentHealth > maxHealth) {
             currentHealth = maxHealth;
         }
     }
     
     void GiveExtraLife() {
         currentHealth++;
         currentScore = 0;
     }
     
     void OnTriggerEnter(Collider col) {
         if (col.tag == "Collectible") {
             currentScore++;
             Destroy(col.gameObject);
          }
     }
     
     void GameOver() {
         
     }
 }


PlayerAttack.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
     public GameObject enemy;
 
     void Start () {
         
     }
     
     void Update () {
         if(Input.GetMouseButtonUp(0)) {
             Attack();
         }
     }
     
     void Attack() {
         float dis = Vector3.Distance(enemy.transform.position, transform.position);
         
         Debug.Log(dis);
         
         if(dis <= 1.3) {
             Enemy eh = (Enemy)enemy.GetComponent("Enemy");
             eh.currentHealth--;
         }
     }
 }
 
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 PAEvenson · Dec 26, 2012 at 06:05 PM 0
Share

Is the "Is Trigger" box checked in the inspector for the collider?

avatar image Lovrenc · Dec 26, 2012 at 06:09 PM 0
Share

Add another collider on player character with "Is Trigger" checked or move player health code into "OnCollisionEnter" function.

avatar image Eltore · Dec 26, 2012 at 08:40 PM 0
Share

Yes it is. And if I'll move the collision part, it will cause an error, because "OnCollisionEnter" is already used. I did try that before.

avatar image Lovrenc · Dec 26, 2012 at 09:49 PM 0
Share

You can have more than one functionality inside "OnCollisionEnter" event function...

1 Reply

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

Answer by Eltore · Dec 27, 2012 at 01:54 PM

Solved. I'm the stupidiest person ever. I realized, why I got that error. It's because I did it like this:

 OnTriggerEnter(Collider col) {
    if(col.tag == "Example") {
       DoSomething()
    }
 }
 
 OnTriggerEnter(Collider coll) {
    if(coll.tag == "Example_two") {
       DoSomething()
    }
 }


Instead of this:


 OnTriggerEnter(Collider col) {
    if(col.tag == "Example") {
       DoSomething()
    }
    
    if(col.tag == "Example_two") {
       DoSomething()
    }
 }
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

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

11 People are following this question.

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

Related Questions

Enemy not taking damage on collisions. 2 Answers

Decreasing Player Health On Collision with Enemy 2 Answers

Adding a counter? 1 Answer

How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer

How to make my player not lose health when attacking the enemy? 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