- Home /
Using Health Potion to Increase health
Good day everyone! I'm still relatively new to Unity and I'm working on a 3D RPG game by following Brackey's tutorial on Youtube.
I'd like to implement a health potion that will increase the player's health, however I've ran into a bit of a problem. When I use the potion from my inventory it doesn't do anything.
Here's my HealthPotion script that derives from my Item script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "HealthPotion", menuName = "Inventory/Potion")]
public class HealthPotion : Item
{
public int health;
public GameObject player;
public override void Use()
{
base.Use();
player.GetComponent<CharacterStats>().currentHealth += health;
Debug.Log("Increasing health by " + health);
}
}
Item script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
new public string name = "New Item"; // name of the item
public Sprite icon = null; // item icon
public bool isDefaultItem = false; // is the item default wear
public virtual void Use()
{
// Use the item
// Something might happen
Debug.Log("Using " + name);
}
// removes the item from inventory when it's equipped
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
CharacterStats script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterStats : MonoBehaviour
{
public int maxHealth = 100; // default max health;
public int currentHealth; // current health
public Stat damage; // damage value
public Stat armor; // armor value
void Awake()
{
currentHealth = maxHealth;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.T))
{
TakeDamage(10);
}
}
public void TakeDamage(int damage)
{
damage -= armor.GetValue();
damage = Mathf.Clamp(damage, 0, int.MaxValue); // so that the damage never goes below 0
currentHealth -= damage; // decreasing health of character whenever they take damage
Debug.Log(transform.name + " takes " + damage + " damage");
// if health is below 0, the player or the enemy will die
if (currentHealth <= 0)
{
Die();
}
}
public virtual void Die()
{
// Die in some way
// This method is meant to be overwritten
Debug.Log(transform.name + " died");
}
}
When I use the potion, it successfully outputs the Debug.Log statement in the HealthPotion script however it doesn't actually apply the change to my current health. :( Where am I going wrong?
That's weird, in theory that should work. To make your code a little bit cleaner, you could directly reference on CharacterStat script instead of GameObject and then do some GetComponent calls. Then just like how the "TakeDamage" was done, you could make "IncreaseHealth ()" method on your CharacterStat script then put your Debug Log on it to check if it is actually working
public int health;
public CharacterStats characterStats;
public override void Use(){
base.Use();
characterStats.IncreaseHealth(health);
}
---------- ON CharacterStats script
public void IncreaseHealth(int amount){
currentHealth += amount;
Debug Log(amount);
}
So I tried doing it this way, but now it's giving me a "NullReferenceException: Object reference not set to an instance of an object" error on the characterStats.IncreaseHealth(health);
line in my HealthPotion script :(
I think that's because the Potion is a Scriptable object and to reference the player is to search it like FindObjectOfType or setup a Singleton. I can't test it right now I'm sorry
Answer by Jakems · Jan 02, 2021 at 05:13 PM
Where are you assigning the "player" Variable on the HealthPotion script?
I decided to get rid of the player variable completely since the potion is a scriptable object and when I tried to assign the player it wouldn't let me (HealthPotion isn't attached to any game object).
So what I did was I created a singleton in my CharacterStats script in order to access the variables and methods easily from my HealthPotion script.
Then I created a simple method in my CharacterStats script that increases the player's health by 10.
Now, the problem starts in the HealthPotion script. This is what I did:
PlayerStats.instance.IncreaseHealth(health);
PlayerStats is the script that's attached to the player game object and it inherits the CharacterStats script.
However this doesn't do anything. It doesn't increase my health, and I'm not quite sure what I'm doing wrong
Answer by MomkeyDev · Jan 04, 2021 at 12:06 AM
Alright i know this isn't helping, but have you actually put a number in the health int on the Healthpotion ?
Hi! Yes I have haha :)) I even double (triple) checked to see if that was the problem but it wasn't.
Turns out I shouldn't have used singletons in this case in the first place since my CharacterStats script is attached to multiple game objects (Player and Enemy). After using Debug.Log() in basically everywhere in my code, I found out that the HealthPotion script was actually increasing the Enemy's health and not the Player's since it didn't know which game object I was referring to. :)) I fixed this issue by not using singletons at all and instead, I tagged my player with the 'Player' tag and referred to the player game object in code by using FindGameObjectWithTag<>
. The rest was easy as I finally had a proper reference to my player game object. :D I'll upload my full solution in the morning (It's very late right now).
Oh god, i know your pain. 1 tips i can give to you next time is to use gameObject.name for debugging if you wanna change value on an object.
Thank you for the tip! I'll definitely try that out next time. :D
Hello, Did you manage to find an answer for this? I am at the same place as you were and I'm stuck, I have been searching for an answer for a couple days and can't seem to find one. I've tried many solutions myself but can't seem to get anything to function properly. Any help would be appreciated. Thanks in advance.
Your answer
Follow this Question
Related Questions
Enemy Health, Player Damage 0 Answers
3D Isometric Grid Based System, a feast of questions; 1 Answer
Health System 2 Answers
Fog of War with Line of Sight comprehension 0 Answers
3d Object in GUI Layer 2 Answers