Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Assets\Scripts\HealthController.cs(22,49): error CS1003: Syntax error, ',' expected

Discussion in 'Getting Started' started by philip_palm, Apr 1, 2024.

  1. philip_palm

    philip_palm

    Joined:
    Oct 16, 2023
    Posts:
    1
    pls help me with this error
    Assets\Scripts\HealthController.cs(22,49): error CS1003: Syntax error, ',' expected

    code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class HealthController : MonoBehaviour
    {
    [Header("Player Health Amount")]
    public float currentPlayerHealth = 100.0f;
    [SerializeField] private float maxPlayerHealth = 100.0f;
    [SerializeField] private int regenRate = 1;
    private bool canRegen = false;

    [Header("Add the Splater image here")]
    [SerializeField] private Image redSplatterImage = null;

    [Header("Hurt Image Flash")]
    [SerializeField] private Image hurtImage = null;
    [SerializeField] private float hurtTimer = 0.1f;

    [Header("Heal Timer")]
    [SerializeField] private float healCooldown 3.0f;
    [SerializeField] private float maxHealCooldown = 3.0f;
    [SerializeField] private bool startCooldown = false;

    [Header("Audio Name")]
    [SerializeField] private AudioClip hurtAudio = null;
    private AudioSource healthAudioSource;

    private void Start()
    {
    healthAudioSource = GetComponent<AudioSource>();
    }

    void UpdateHealth()
    {
    Color splatterAlpha = redSplatterImage.color;
    splatterAlpha.a = 1 - (currentPlayerHealth / maxPlayerHealth);
    redSplatterImage.color = splatterAlpha;
    }

    IEnumerator HurtFlash()
    {
    hurtImage.enabled = true;
    healthAudioSource.PlayOneShot(hurtAudio);
    yield return new WaitForSeconds(hurtTimer);
    hurtImage.enabled = false;
    }

    public void TakeDamage()
    {
    if(currentPlayerHealth >= 0)
    {
    canRegen = false;
    StartCoroutine(HurtFlash());
    UpdateHealth();
    healCooldown = maxHealCooldown;
    startCooldown = true;
    }
    }

    private void Update()
    {
    if(startCooldown)
    {
    healCooldown -= Time.deltaTime;
    if(healCooldown <= 0)
    {
    canRegen = true;
    startCooldown = false;
    }
    }

    if(canRegen)
    {
    if (currentPlayerHealth <= maxPlayerHealth - 0.01)
    {
    currentPlayerHealth += Time.deltaTime * regenRate;
    UpdateHealth();
    }
    else
    {
    currentPlayerHealth = maxPlayerHealth;
    healCooldown = maxHealCooldown;
    canRegen = false;
    }
    }
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,657
    Please edit your post to use code-tags when posting code and not plain text.

    The error tells you the line (22) and column numbers (49) so you only have to look there to see why it's expecting a comma.

    Around that area you'll have a typo so just look at the code carefully or compare it to where you're copying the code from.

    The compiler might expect something but it's nearly always because you made it expect that as you made a typo so look at the area but don't take what the compiler expects as what's wrong.
     
    Ryiah likes this.