Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Jun 23, 2017 at 11:51 AM by Ben_Huang4163 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Ben_Huang4163 · Jun 20, 2017 at 03:37 AM · scripting problemitem pickuphealth

pick item and add health script have problem

Hello: I got this problem when i'm designing my player health system, this is my script attached to the player:

  public class PlayerHealth : MonoBehaviour {
     
         public int startingHealth = 100;
         public int currentHealth;
       
         void Awake () {       
             health.Initialize();
             health.MaxVal = startingHealth;        
         }
         void Start()
         {
             currentHealth = startingHealth;     
         }

         void Update () {
              health.CurrentVal = currentHealth; 
         }
     
         public void TakeDamage(int amount)
         {
             if (isDead==true)
             {
                 return;
             }
             isDamage = true;
             currentHealth -= amount;
             animt.SetTrigger("IsDamage");
             if (currentHealth<=0&&!isDead)
             {
                 Deadth();
             }     
         }
 
         private void OnTriggerEnter(Collider other)
         {
             if (other.tag=="Life")
             {
                 if (currentHealth>=startingHealth)
                 {
                     currentHealth = startingHealth;
                }
                 if (currentHealth<startingHealth&&currentHealth>0)
                 {
                     currentHealth += 10; 
                 }
                 if (currentHealth<=0)
                 {
                     currentHealth = 0;
               }
             }
         } 
     }

and this script attached to the add-health item:

 public class AddLifescript : MonoBehaviour {
     
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag=="Player")
         {        
             Destroy(this.gameObject);        
         }
     }
 }

this two scripts did their job just fine, but i found that they are not extensible. if i have more items , i have to add them all to the playerHealth.cs, that is not very effective in my opinion. so i changed them, like this, the script attached to the player:

  public class PlayerHealth : MonoBehaviour {
     
         public int startingHealth = 100;
         public int currentHealth;
     
         [SerializeField]
         private Stat health;
         
         void Awake () {
     
             health.Initialize();
             health.MaxVal = startingHealth;
         }
         void Start()
         {
             currentHealth = startingHealth;     
         }
         
         // Update is called once per frame
         void Update () {
              health.CurrentVal = currentHealth;
          
         }
     
         public void TakeDamage(int amount)
         {
             if (isDead==true)
             {
                 return;
             }
             isDamage = true;
             currentHealth -= amount;
             animt.SetTrigger("IsDamage");
             if (currentHealth<=0&&!isDead)
             {
                 Deadth();
             }     
         }
     
  
 public void AddLife(int addLife)
     {   
             currentHealth += addLife;
             Debug.Log(currentHealth);
         if (currentHealth >= startingHealth)
         {
             currentHealth = startingHealth;
         }
 
         if (currentHealth <= 0)
         {
             currentHealth = 0;
         }
     }        
     }
 

the script attached to item:

 public class AddLifescript : MonoBehaviour {
     public GameObject player;
    PlayerHealth playerHealth;
    public int healthAdd = 10;
 
     private void Awake()
     {
        playerHealth = player.GetComponent<PlayerHealth>();
     }
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag=="Player")
         {
             playerHealth.AddLife(healthAdd);            
             Destroy(this.gameObject);        
         }
     }
 }

but these two scrips didn't work very well, the problem is the debug.log in addLife function shows 110,120 when i picked up a health item, but the currentHealth remain the same amount , has no changes. the the health slider has no change too. this really bother me. thank you if anyone can help me with this .

---------more detail about my problem: the whole AddLife() function just don't work correctly. if the player's health is 100, i pick up an item, the debug log will show "110" , not 100. if the player got hurt, the health slider decreased, it work no problem, display correctly, the player will even die if the health slider is equal to 0. but no matter how many items i picked up, the debug log keep showing "110", not the right health value. and the slider value remains the same, no increase.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by thePeine · Jun 20, 2017 at 03:53 AM

If I understand your intention correctly, it looks like you're just resetting the value when you don't want to. On line 47, the health will be 110. Then, on line 48 you'll log the right value, 110. The problem is on line 49, 110 will be greater than starting health of 100. So you'll reset it to 100. So when the function exits, you haven't actually changed anything.

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 Ben_Huang4163 · Jun 20, 2017 at 05:58 AM 0
Share

sorry, maybe i don't explain myself clearly. i mean the whole AddLife() function just don't work correctly. if the player's health is 100, i pick up an item, the debug log will show "110" , not 100. if if the player's health is lower 100, the health slider part work no problem, display correctly, the player will even die if the health slider is equal to 0. but no matter how many items i picked up, the debug log keep showing "110", not the right health value.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers

I am trying to access a variable from another script. 1 Answer

Need help with burst fire script / adjusting the amount of damage my gun does. 0 Answers

How to Pick up 1 collider Object at a time, Vs. all at once? Item Script 1 Answer

Error: CS0246 Can't access Player Health Script 1 Answer


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