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

Question 2 CS0106s I NEED HELP!!!

Discussion in 'Scripting' started by BBTgames, Mar 14, 2023.

  1. BBTgames

    BBTgames

    Joined:
    Mar 13, 2023
    Posts:
    5
    I am very new to coding and am working on my first health bar. I followed everything on the tutorial and it worked!!! Unfortunately, when i went to the next video where he added text to the bar it showed these 2 errors. I have no clue what they mean and how to fix them because I thought I did everything right. Sorry if it's a lot of code, I just didn't want to miss anything. By the way I'm using TextMeshPro... Thanks!!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. public class PlayerHealth : MonoBehaviour
    7. {
    8.     private float health;
    9.     private float lerpTimer;
    10.     public float maxHealth = 100f;
    11.     public float chipSpeed = 2f;
    12.     public Image frontHealthBar;
    13.     public Image backHealthBar;
    14.     [SerializeField]
    15.     private TextMeshProUGUI healthAmmount;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         health = maxHealth;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         health = Mathf.Clamp(health, 0, maxHealth);
    26.         UpdateHealthUI();
    27.         if (Input.GetKeyDown(KeyCode.X))
    28.         {
    29.             TakeDamage(Random.Range(5, 10));
    30.         }
    31.         if (Input.GetKeyDown(KeyCode.Z))
    32.         {
    33.             RestoreHealth(Random.Range(5, 10));
    34.         }
    35.     }
    36.  
    37.     public void UpdateHealthUI()
    38.     {
    39.         Debug.Log(health);
    40.         float fillF = frontHealthBar.fillAmount;
    41.         float fillB = backHealthBar.fillAmount;
    42.         float hFraction = health / maxHealth;
    43.         if (fillB > hFraction)
    44.         {
    45.             frontHealthBar.fillAmount = hFraction;
    46.             backHealthBar.color = Color.red;
    47.             lerpTimer += Time.deltaTime;
    48.             float percentComplete = lerpTimer / chipSpeed;
    49.             backHealthBar.fillAmount = Mathf.Lerp(fillB, hFraction, percentComplete);
    50.         }
    51.         if (fillF < hFraction)
    52.         {
    53.             backHealthBar.fillAmount = hFraction;
    54.             backHealthBar.color = Color.green;
    55.             lerpTimer += Time.deltaTime;
    56.             float percentComplete = lerpTimer / chipSpeed;
    57.             frontHealthBar.fillAmount = Mathf.Lerp(fillF, hFraction, percentComplete);
    58.         }
    59.         //healthAmmount.text = Mathf.Round(health) + "/" + Mathf.Round(maxHealth);
    60.  
    61.         public void TakeDamage(float damage)
    62.         {
    63.             health -= damage;
    64.             lerpTimer = 0f;
    65.         }
    66.         public void RestoreHealth(float healAmount)
    67.         {
    68.             health += healAmount;
    69.             lerpTimer = 0f;
    70.         }
    71.     }
    72. }
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Please post the line numbers the errors are on. When you highlight an error in the console it will show you what line number it's on, which helps with the debugging process. Perhaps once you see the line numbers you'll be able to fix it without any help.
     
  3. BBTgames

    BBTgames

    Joined:
    Mar 13, 2023
    Posts:
    5
    61,9 + 66,9
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    Even more important, we are humans, not compilers. We don't care or remember error codes. The compiler already gives you an english description of the error. So when you ask questions about an actual error you got, include the actual error and not just the error code. The error would be something like
    Your mistake is that you put your two methods TakeDamage and RestoreHealth INSIDE your "UpdateHealthUI" method. In the past you would have gotten a different error because local methods weren't a thing. Nowadays C# supports local methods. However local methods can not have an access modifier since they can only be used inside the method they are defined in. You most likely don't want local methods but you want them inside the class.
     
    arkano22 and samana1407 like this.
  5. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    204
    It also makes no sense to make a health limit and update its UI interface inside the Update method. Put the logic in a separate method and call it automatically ONLY when health changes.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. public class PlayerHealth : MonoBehaviour
    7. {
    8.     private float health;
    9.     private float lerpTimer;
    10.     public float maxHealth = 100f;
    11.     public float chipSpeed = 2f;
    12.     public Image frontHealthBar;
    13.     public Image backHealthBar;
    14.     [SerializeField]
    15.     private TextMeshProUGUI healthAmmount;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         health = maxHealth;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.X))
    26.         {
    27.             TakeDamage(Random.Range(5, 10));
    28.         }
    29.         if (Input.GetKeyDown(KeyCode.Z))
    30.         {
    31.             RestoreHealth(Random.Range(5, 10));
    32.         }
    33.     }
    34.  
    35.     void UpdateHealthUI()
    36.     {
    37.         Debug.Log(health);
    38.         float fillF = frontHealthBar.fillAmount;
    39.         float fillB = backHealthBar.fillAmount;
    40.         float hFraction = health / maxHealth;
    41.         if (fillB > hFraction)
    42.         {
    43.             frontHealthBar.fillAmount = hFraction;
    44.             backHealthBar.color = Color.red;
    45.             lerpTimer += Time.deltaTime;
    46.             float percentComplete = lerpTimer / chipSpeed;
    47.             backHealthBar.fillAmount = Mathf.Lerp(fillB, hFraction, percentComplete);
    48.         }
    49.         if (fillF < hFraction)
    50.         {
    51.             backHealthBar.fillAmount = hFraction;
    52.             backHealthBar.color = Color.green;
    53.             lerpTimer += Time.deltaTime;
    54.             float percentComplete = lerpTimer / chipSpeed;
    55.             frontHealthBar.fillAmount = Mathf.Lerp(fillF, hFraction, percentComplete);
    56.         }
    57.         //healthAmmount.text = Mathf.Round(health) + "/" + Mathf.Round(maxHealth);
    58.  
    59.     }
    60.     public void TakeDamage(float damage)
    61.     {
    62.         health -= damage;
    63.         UpdateHealthStuff();
    64.     }
    65.     public void RestoreHealth(float healAmount)
    66.     {
    67.         health += healAmount;
    68.         UpdateHealthStuff();
    69.     }
    70.  
    71.     private void UpdateHealthStuff()
    72.     {
    73.         health = Mathf.Clamp(health, 0, maxHealth);
    74.         lerpTimer = 0f;
    75.  
    76.         UpdateHealthUI();
    77.     }
    78. }
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    Well, not really as he is triggering a lerp towards the actual value when the health changes. The lerp has to happen every frame. He uses Time.deltaTime inside UpdateHealthUI. So calling it once wouldn't make much sense. I do understand your way of thinking and it's usually correct. However the animation he aims for would not work this way. He could use coroutines to pull off the animation, however this gives you tons of potential issues when you have overlapping behaviour. So having the animation in Update is one of the simplest solutions.

    Instead of those two Lerps, it would be simpler to use a MoveTowards call instead to do the animation.
     
  7. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    204
    You're right, I didn't even notice the animation inside the UpdateHealthUI method..! Thanks for the correction.
     
  8. BBTgames

    BBTgames

    Joined:
    Mar 13, 2023
    Posts:
    5
    Thanks for all of your help!! It works now!!!