Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Beginner: Basic Health Managing Script

Discussion in 'Scripting' started by Subtronaut, Feb 13, 2016.

  1. Subtronaut

    Subtronaut

    Joined:
    Feb 13, 2016
    Posts:
    2
    Hi guys,

    two weeks ago in started to venture into the world of Unity and scripting. As i'm a complete beginner i followed some tutorials on Unity Learn and achieved some basic understanding of how to code in C#.
    Now rewrite some code i found in tutorials and try to write it by myself. Right now i try to attach a script which should inflict "damage" to my player onto a cube and a "health managing system" to my player.

    PlayerHealthManager
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerHealthManager : MonoBehaviour
    6. {
    7.  
    8.     public int MaximumHealth;
    9.     public int CurrentHealth;
    10.  
    11.     void Start ()
    12.     {
    13.         CurrentHealth = MaximumHealth;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (CurrentHealth <= 0)
    19.         {
    20.             gameObject.SetActive (false);
    21.         }
    22.     }
    23.  
    24.     public void DamageToPlayer (int damageToGive)
    25.     {
    26.         CurrentHealth = CurrentHealth - damageToGive;
    27.     }
    28. }
    29.  
    DamageToPlayer
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DamageToPlayer : MonoBehaviour
    6. {
    7.     public int damageToGive;
    8.  
    9.     void OnCollisionEnter (Collision other)
    10.     {
    11.         if (other.gameObject.name == "Player")
    12.         {
    13.             other.gameObject.GetComponent<PlayerHealthManager> ().DamageToPlayer (damageToGive);
    14.         }
    15.     }
    16. }
    17.  
    I attached the "DamageToPlayer" script to a cube of which i even enlarged the collision box.
    I also attached the "PlayerHealthManager" to my FPS.

    Player is named & tagged Player for ease.
    damageToGive is set, as are the other variables.

    Still if i run into the collider box of the cube absolutely nothing happens. And i don't get why :(


    Edit: It seems even i can not get the collision detection to work.
     
    Last edited: Feb 13, 2016
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Try adding a rigidbody to the player (you can freeze it) then setting the (I think) "Collision Type" to continous dynamic.
     
  3. Subtronaut

    Subtronaut

    Joined:
    Feb 13, 2016
    Posts:
    2
    I found a way around using OnTriggerEnter. But i'll soon get back to it and try your resolution. Thank you!
     
  4. frilanski

    frilanski

    Joined:
    Oct 19, 2013
    Posts:
    11
    i did just see on another post that OnTriggerEnter only checks once like a start void and there solution was OnTriggerStay which i assume is a version of OnTriggerEnter but it checks on update rather than once.

    I assume that was your way around it but if not worth a shot

    Good Luck!;)
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It checks right on contact, then it stops checking, in case of other contacts it checks again. OnTriggerStay does the same, but it continues to run until the contact is gone
     
  6. frilanski

    frilanski

    Joined:
    Oct 19, 2013
    Posts:
    11
    ok well I'm outa ideas haha