Search Unity

My code doesnt work

Discussion in 'Scripting' started by Ali_Fuat_ADAK1, Jan 15, 2022.

  1. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    So i was working on my health system to test i assigned taking damage to E but it doesnt work and i couldnt find any spelling mistakes

    My HealthBar Code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class HealthBar : MonoBehaviour
    {
    [SerializeField] private Health playerHealth;
    [SerializeField] private Image totalhealthBar;
    [SerializeField] private Image currenthealthBar;

    private void start()
    {
    totalhealthBar.fillAmount = playerHealth.currentHealth / 10;
    }
    private void update()
    {
    currenthealthBar.fillAmount = playerHealth.currentHealth / 10;
    }
    }


    My Health Code:
    using UnityEngine;

    public class Health : MonoBehaviour
    {

    public float currentHealth { get; private set; }
    [SerializeField] private float startingHealth;

    // Start is called before the first frame update
    private void Awake()
    {
    currentHealth = startingHealth;
    }
    public void TakeDamage(float _damage)
    {
    currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);

    if (currentHealth > 0)
    {

    }else{

    }
    }

    // Update is called once per frame
    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.E))
    TakeDamage(1);
    }

    }
     
    Last edited: Jan 15, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    A few things. This is the 2D forum for 2D related issues and questions. Your post is a pure scripting post so is best suited to the dedicated scripting sub-forum here. Also, when posting code, please use code-tags and not plain text but you have been told this before so can you please edit your post to change this.

    Anyway, I'll move your post for you.

    As a side-note, "doesn't work" isn't a description of a problem. A bit of friendly advice here; if you want help then please spend a little effort in describing what you expect to happen, what actually happens and what you've done to try to narrow down the problem. If you don't do this then it just looks like you've done nothing to figure it out and just want others to do it for you.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    As can be seen in your previous post, you're not looking at the case of things. It's not "start", it's "Start" along with "update" and other stuff.

    In one script you use uppercase, in the other you don't. You need to be exact and consistent.
     
  4. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    oh thats it i am so stupid lol
     
  5. Ali_Fuat_ADAK1

    Ali_Fuat_ADAK1

    Joined:
    Dec 1, 2021
    Posts:
    24
    thanks man
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Maybe you can try to edit your post to use code-tags? That way, you'll know next time you post code. ;)