Search Unity

Health Pickup

Discussion in 'Scripting' started by The3DKnight, Aug 20, 2011.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Guys i have this script for health control but it doesn't seem to work can some one help?

    Code (csharp):
    1.  function OnControllerColliderHit(hit : ControllerColliderHit)
    2.     {
    3.     if(hit.gameObject.tag == "healthpickup")
    4.     {
    5.     Destroy(hit.gameObject);  // Destroys Health Box
    6.     print("Gained 10 Health");
    7.     HealthControl.LIVES +=6 ;
    8.     }
    9.    }
     
  2. xRyanx

    xRyanx

    Joined:
    Jan 22, 2011
    Posts:
    10
    Not very good with scripting but I guess I could try and help you fix it. Below is the way that I personally would do it and below the code I will explain it a little.

    Code (csharp):
    1. var Script : HealthControl; //This allows you to place the HealthControl script in the Inspector.
    2.  
    3. function OnTriggerEnter(){ //Personally i think it's easier if we use OnTriggerEnter.
    4.  
    5.     Destroy(gameObject);  // Destroys Health Box
    6.     print("Gained 10 Health");
    7.     Script.LIVES +=10; // Script is the HealthControl script that we added in the Inspector.
    8.    
    9.     }
    So this is the Script that will be placed onto the HealthPickup GameObject. All you have to do is make sure that you have "Is Trigger" selected on this object.

    This next piece of code will be the GUI Script that will tell us how many lives we currently have. This can be customized with your own skin or icon. You will add this Script to the main camera. Then you will drag the main camera into the "Script" slot of the previous code in the Inspector. This Script must be named "HealthControl".

    Code (csharp):
    1. var LIVES: int = 10; //This is the number of lives which can be changed from the Inspector.
    2.  
    3.  
    4.  
    5. function OnGUI () {
    6.  
    7.    
    8.  
    9.     GUI.Label (Rect (600, 10, 100, 20), "Lives: "+LIVES); //This is just a label telling us how many lives we have.
    10.  
    11.    
    12.  
    13.      }
    14.  
    15.    
    This should work fine and I hope it helps you. Good luck with the rest of your project and if you have any problems with this script just let me know and I'll try my best to help you if I can.
     
    Last edited: Aug 20, 2011
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Some more information would be nice. What is it doing?

    Your character needs a CharacterController
    The pickup needs to be tagged "healthpickup"
     
  4. Jupp

    Jupp

    Joined:
    Aug 18, 2011
    Posts:
    13
    More details, what doesn't work ? does the function even fire off ? do you even get the print output?
    More details are needed :)
     
  5. MathewRyanWandell

    MathewRyanWandell

    Joined:
    Nov 21, 2019
    Posts:
    6
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I also want to know why he's trying to use Javascript when it's deprecated on Unity now.
     
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    This thread is from 2011.

    You also need some manners and your own thread.
     
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh wow my bad I didn't even notice this was a blatant necro lol.