Y
Y
Yuri2020-06-29 14:12:43
C++ / C#
Yuri, 2020-06-29 14:12:43

How to describe first-aid kits correctly in the player's script so that they add health?

Create a new type of level bonus - first aid kits, which can store different amounts of healing. When a first aid kit comes into contact with any object containing a Health component, the first aid kit should add an amount of healing to the object and disappear from the game scene.
The player contains a Health component. I need to call the method in the player script

public void SetHealth(int bonusHealth)
{

health+= bonusHealth;

}

and thereby add bonus health to the player's basic health.
public void OnTriggerEnter2D(Collider2D col)
 {
   if(col.gameObject.CompareTag("Tablet"))
   {
     Destroy(col.gameObject);
   }
 }

At the moment, my first-aid kits are being destroyed, but they are not adding health. Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-06-29
@uriy99

Maybe you need to call a method in a trigger? It won't work just by writing it:

public void SetHealth(int bonusHealth)
{
health+= bonusHealth;
}

public void OnTriggerEnter2D(Collider2D col)
 {
   if(col.gameObject.CompareTag("Tablet"))
   {
     Destroy(col.gameObject);
     SetHealth(10);//Вместо 10 укажи нужное тебе бонусное хп.
   }
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question