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

OnCollider script

Discussion in 'Scripting' started by Teemo, Dec 22, 2014.

  1. Teemo

    Teemo

    Joined:
    Jun 20, 2013
    Posts:
    15
    I have this script on my car:
    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4. publicvar hpPoints :float=3;
    5. publicvarCurrentHP:float= hpPoints;
    6. functionStart(){
    7. }
    8. functionUpdate(){
    9. }
    10. functionOnCollisionEnter(collision:Collision){
    11. if(collision.gameObject.tag =="obstacle"){
    12. CurrentHP-=1;
    13. }
    14. print("You have hit an object. "+"You have "+CurrentHP+"/"+ hpPoints +" HP points.");
    15. if(CurrentHP==0){
    16. print("You lost!");
    17. }
    18. }
    19.  
    20.  
    The collider works, but everytime I hit an "obstacle", it still says "You have 3/3 HP points", instead of 2/3 or 1/3 or 0/3 ("You lost!"). I think it is a syntax problem. Does someone know ?
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Try turning off your collider momentarily, then turning it back on.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Log the tag of your collider to ensure that it's "obstacle". Your log isn't in the if but the deduction of HP is so the collision is working but the tag is most likely incorrect.
     
  4. Teemo

    Teemo

    Joined:
    Jun 20, 2013
    Posts:
    15
    Hi. Thank's for replying. Actually you are right, the tag was "Obstacle" instead of "obstacle" but now, I got another problem. When I start the game, without hiting anything, it gives me this:



    The first one is 0/3 and the last 3/3 . Why ? Also, when I hit an obstacle, the first become -1/3 and the last 2/3, and so on.
    I also modified my script a little bit:
    Code (csharp):
    1.  
    2. #pragma strict
    3. public var hpPoints : float = 3;
    4. public var CurrentHP : float = 3;
    5.  
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13. }
    14.  
    15. function OnCollisionEnter(collision: Collision) {
    16.  
    17. if (collision.gameObject.tag == "obstacle"){
    18.    CurrentHP -= 1;
    19. }  
    20. print("You have hit an object. " + "You have " + CurrentHP + "/" + hpPoints + " HP points.");
    21.  
    22. if (CurrentHP <= 0){
    23.   print("You lost!");
    24.  }
    25.  
    26. }
    27.  
    What can it be now ?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You must be hitting something to trigger it.

    Negative numbers would be expected because you're not doing anything to stop the subtraction of health when the health is already at 0. Also - if you're adding/subtracting whole numbers just use integers instead of floats.
     
  6. Teemo

    Teemo

    Joined:
    Jun 20, 2013
    Posts:
    15
    I know I have to hit something to trigger it, but as I said, it told me that I lost WITHOUT hiting anything, which is wrong.
    I think the problem is my script, not how I use it.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That was my point. Unless you have code elsewhere that is causing a "loss" then you *are* hitting something :)