Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make a working Health bar with java?

Discussion in 'Scripting' started by zwhistle, Mar 7, 2015.

  1. zwhistle

    zwhistle

    Joined:
    Mar 6, 2015
    Posts:
    3
    Hey, i'm relatively new, and i'm still learning to program. i've started with java, and i've gotten to the point where i need to create a health bar in the RPG i'm making. (small scale, don't worry, i'm not over encumbering myself) Every tutorial is either in C# or does not explain it enough for me to understand it. I want to stick with java for this game, as all of the current scripts are written with it. i have a health bar texture and a frame for it, done in Photoshop, and i want to know how to make the bar shrink and regrow inside the frame when i take damage or regain health.

    (i have currently set the health bar and frame up on a canvas. i can remove it, but i don't know whether this information is relevant or not)

    Here is my health script:

    #pragma strict

    var Health = 100;
    var Health2 = 100;

    function Update ()
    {

    }

    function ApplyDamage (TheDamage : int)
    {
    Health -= TheDamage;

    if(Health <= 0)
    {
    Dead();
    }
    }

    function Dead ()
    {
    RespawnMenu1.playerIsDead = true;
    Debug.Log("Player Died");
    }

    If you can guide me through how to get the texture to shrink according to the health and damage taken, I would be very grateful. thank's for taking the time to read this.
     
  2. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    You need to call your functions in the update.

    Also, you may want to use variable names that are easier to remember, like "maxHealth" and "currentHealth".

    From the looks of it though, I would recommend doing the Unity tutorials again. And I would recommend switching to C# since it's becoming the unwritten standard for Unity.
     
    elmar1028 likes this.
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    1) It's UnityScript not Java. Keep that in mind
    2) Always google before asking questions on forums to avoid duplicates. Assuming you're using a new UI, a 5 second search gave me a nice video:



    3) And what @Not_Sure just said: if you want to check variables "every frame" then put your code into the Update() function.
     
  4. zwhistle

    zwhistle

    Joined:
    Mar 6, 2015
    Posts:
    3
    I just stated that i want it in java. this is C#. i know maybe it's inefficient, but i want it in java. Next, just saying UnityScript applies to every language. i specifically stated java, not C# for the fifth time. Next, i searched the internet for 3 hours last night. i did check, and i found nothing that was in Java or that i understood. This video included. *COUGH* C#.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Unity doesn't use Java anywhere.

    You can use C#, unityscript, or boo (though boo is being phased out).

    Unityscript is a custom language by unity that is similar to javascript.

    Note, I said javascript, NOT Java. Java and javascript are very different things.
    https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java vs javascript


    Also, learn to wrap your code in the code tags when you post in the forum, it makes reading your code much easier.



    And lastly...

    https://www.google.com/webhp?source...espv=2&ie=UTF-8#q=unity javascript health bar

    Look at that... so many tutorials. Approaching it from so many different methods. And so many using unity/javascript!
     
    Last edited: Mar 7, 2015
    ADNCG likes this.
  6. zwhistle

    zwhistle

    Joined:
    Mar 6, 2015
    Posts:
    3
    Alright. didn't know the difference between java and javascript. thank you. But, no, almost all of these are actually C# or i don't understand them. So, rather than giving me a link, i would appreciate direct help.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Use a slider from the new UI system. Set slider.value to health inside of ApplyDammage.
     
    lordofduct likes this.
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    You're what I like to call an indignant little...

    First link, javascript.

    Second link, answers catalogue page with several links that use javascript.

    Third link, video tutorial that uses javascript.

    Fourth link, video tutorial, uses C#... I guess google isn't perfect.

    Fifth link, uses javascript.

    OHHHHHH, I get it... you're a complete and utter newb looking for us to just hand feed you the code rather than you take the time and effort to follow a tutorial or read a book and LEARN something.

    Move along, you don't belong here.
     
    ADNCG likes this.