#My healing system doesn't work

1 messages · Page 1 of 1 (latest)

bitter osprey
#
using System.Collections.Generic;
using UnityEngine;
using System;

public class PlayerHealth : MonoBehaviour
{
    public static event Action OnPlayerDeath;
    public int currentHealth;
    public int maxHealth = 3;
    // Start is called before the first frame update
    void Start()
    {
        currentHealth = maxHealth;
    }
    
  public void TakeDamage (int amount)
    {
        currentHealth -= amount;
        if(currentHealth <=0)
        {
            Destroy(gameObject);
            OnPlayerDeath?.Invoke();
        }
    }

    public void Heal(int amount)
    {
        if (Input.GetKeyDown(KeyCode.Space));
        currentHealth += amount;
        if (currentHealth > maxHealth)
        {
            currentHealth = maxHealth;
        }
    }
}```  This is my player health script, you are supposed to increase your current health by 1 when you press space until reaching the max health of 3 but when I test this out the health value does not change
shell raft
#

show the script you use this Heal method

bitter osprey
#

wdym?

waxen sigil
#

So besides Start, no methods are being called. You need to create a method called "Update". Like how Start is called when you hit Play, Update is one of those magic methods that called every frame. In the Update method you have to check for input and then if the input is recieved, trigger whatever other method it needs to be called. I'll give you an example...

public class MyClass : MonoBehaviour
{
  
  void Start()
  {
    //set some default values or something
   }

   void Update()
   {
    if(Input.GetKeyDown(Keycode.Space))
    {
      Heal();
     }
    }

  void Heal()
  {
    // your heal code here
  }
}

also re-read how to write if/else statements in C# bc it's incorrect in the code you posted

bitter osprey
#

Would I put this in a new code separate from my current code?

#

nvm

#

this is the healing code, I now have the error "Invalid expression term 'int'" on line 18.

#
  {
    public int currentHealth;
    public int maxHealth = 3;

    void Start()
    { 
        currentHealth = maxHealth; }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Heal(int);
        }
    }

    void Heal(int amount)
    {
    currentHealth += amount;
    if (currentHealth > maxHealth)
    {
        currentHealth = maxHealth;
    }
}
}
rustic plover
#

umm, int is a type

#

you would probably have to have a field healSpeed or something, and then use that

#

you would probably want that to be a float as well, not an int

bitter osprey
#

I changed int to float and now I have the same error, just with float, and on line 22 "Identifier expected"

waxen sigil
#

your "Heal" method is expecting a value that is the type of "int", you're trying to pass in the type "int". It's like asking " how much should i heal?" and you're saying "integer" lol

#

pass in a value, either one stored in a variable or you can hard code a value like 1

rustic plover
#

Maybe look up the difference between a type and a variable before you continue

bitter osprey
#
using System.Collections.Generic;
using UnityEngine;

public class PlayerHeal : MonoBehaviour
  {
    public int currentHealth;
    public int maxHealth = 3;
    public int healAmount = 1;

    void Start()
    { 
        currentHealth = maxHealth; }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Heal(1);
        }
    }

   public void Heal(1)
    {
    currentHealth += amount;
    if (currentHealth > maxHealth)
    {
        currentHealth = maxHealth;
    }
}``` Ok so now I only have the compiler error "Identifier expected" on (23,21)
waxen sigil
#

When you define parameters for a method in C#, you must define what type of variable the parameter is and then give it a name

#

then when you invoke that method you have to pass in a variable or value of the type specified

#

so if i were to write a method to add 2 numbers and then call it, it would look like this...

void AddNumbers(int a, int b)
{
    int result = a + b;
}

void Start()
{
  AddNumbers(1,2)
}
#

so in this example the "AddNumbers" takes in 2 parameters, one called "a" and one called "b", both of type int. Then when i call "AddNumbers" and pass in the parameters, i'm passing in two values that are of type int (in this case the numbers 1 and 2)

bitter osprey
#

Ok so if I understand correctly I need to create a new method and use it to assign a value to the heal?

waxen sigil
#

you're not understanding correctly, i'll try to explain

#

when you define a method in C# it looks like this

void Add(int a, int b)
{

}

this is defining the method, it's saying what the method is called, what type of values it need and if it returns any values.

where it says "(int a, int b) its saying that the methods needs an integer value that it's naming "a" and an integer value that it's naming "b". You must use a type, then a name for the variable when defining a method.

bitter osprey
#

Oooooooooh