Search Unity

How to use a value from another script

Discussion in 'Getting Started' started by Sanzeh, Jan 12, 2020.

  1. Sanzeh

    Sanzeh

    Joined:
    Jan 11, 2020
    Posts:
    2
    Hello everybody,

    I'm completely new to unity and coding and i'm trying to set a health bar using the health percentage of my player to adjust the fill Amount of the bar which is causing me trouble. Here is what i started doing is the health bar script.

    Thanks in advance for your time !

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class health : MonoBehaviour
    7. {
    8.  
    9.     private Image content;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         content = GetComponent<Image>();
    16.         content.fillAmount = ();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.        
    23.     }
    24. }
    25.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Saying "health percentage of my player" implies that you have some script that represents this. Hopefully it's on the same GameObject as the script above. Then you can just get a reference to that script with something like

    Code (CSharp):
    1. health = GetComponent<PlayerHealth>();
    in your Start event, and then in Update, do something like

    Code (CSharp):
    1. content.fillAmount = health.curHP / health.maxHP;
    Exact details will of course depend on how your player health script is written, but this should give you the idea.
     
    Sanzeh likes this.
  3. Sanzeh

    Sanzeh

    Joined:
    Jan 11, 2020
    Posts:
    2
    Hi,

    Thanks for your answer. My PlayerHealth script is in player while the health script for the bar is in my UIcanva.
    I tried to do what you advise but i have an error on the GetComponent : "health is a type but is used as a variable".

    Here is the state of my 2 script for now :

    health script (health bar)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class health : MonoBehaviour
    7. {
    8.  
    9.     private Image content;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         health = GetComponent<PlayerHealth>;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         content = GetComponent<Image>();
    22.         content.fillAmount = health.currentHealth / health.maxHealth;
    23.     }
    24. }
    25.  

    PlayerHealth script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHealth : MonoBehaviour
    6. {
    7.     public int maxHealth = 100;
    8.     int currentHealth;
    9.     int healthPercent;
    10.  
    11.     [SerializeField] private health health;
    12.  
    13.     public Healthbar healthBar;
    14.  
    15.     public GameObject deathEffect;
    16.  
    17.     private void Start()
    18.     {
    19.         currentHealth = maxHealth;
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         healthPercent = currentHealth / maxHealth;
    25.     }
    26.  
    27.     public void TakeDamage(int damage)
    28.     {
    29.         currentHealth -= damage;
    30.  
    31.         if (currentHealth <= 0)
    32.         {
    33.             Die();
    34.         }
    35.     }
    36.  
    37.  
    38.     void Die()
    39.     {
    40.         Debug.Log("Enemy died!");
    41.         Instantiate(deathEffect, transform.position, Quaternion.identity);
    42.         Destroy(gameObject);
    43.     }
    44.  
    45.  
    46. }
    47.  
    48.  
    As you can see in player health i tried to link the health bar with the PlayerHealth script but i believe it's not the right method.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, `health` should be a property, not the name of your class (please follow Unity's standard of always starting classes with an uppercase letter, and starting properties, parameters, and local variables with a lowercase letter). I didn't even notice that your health bar script was called health; please change that to something like HealthBar.

    I didn't show the declaration of `health`, but hoped you would figure out to add something like
    PlayerHealth health;
    to the properties area of your script.

    However the deeper problem here is that you don't have your PlayerHealth script and your HealthBar script on the same object. That's going to complicate things a bit. My suggestion:

    1. Move your HealthBar script onto the player object (the same GameObject that has PlayerHealth). This will make GetComponent<PlayerHealth> actually work.
    2. Change "private" to "public" on your declaration of content in the HealthBar script.
    3. Use the Inspector to drag the health bar Image into the content slot of your HealthBar script.
    That should do it!