• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nursedayuksel · Jan 02, 2021 at 02:57 PM · 3drpghealth

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?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_HTr7mOeTIRzvVA · Jan 02, 2021 at 03:39 PM 1
Share

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);
 }
avatar image nursedayuksel unity_HTr7mOeTIRzvVA · Jan 02, 2021 at 03:51 PM 0
Share

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 :(

avatar image unity_HTr7mOeTIRzvVA nursedayuksel · Jan 02, 2021 at 04:37 PM 1
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jakems · Jan 02, 2021 at 05:13 PM

Where are you assigning the "player" Variable on the HealthPotion script?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nursedayuksel · Jan 02, 2021 at 05:56 PM 0
Share

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

avatar image
0

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 ?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nursedayuksel · Jan 04, 2021 at 01:32 AM 0
Share

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).

avatar image MomkeyDev · Jan 05, 2021 at 12:20 AM 1
Share

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.

avatar image nursedayuksel MomkeyDev · Jan 05, 2021 at 01:07 AM 0
Share

Thank you for the tip! I'll definitely try that out next time. :D

avatar image JamesdavidK · Mar 21 at 02:00 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

217 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Isometric Grid Based System, a feast of questions; 1 Answer

Enemy Health, Player Damage 0 Answers

Health System 2 Answers

Fog of War with Line of Sight comprehension 0 Answers

3d Object in GUI Layer 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges