Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by CerebralStatic · Mar 11, 2020 at 02:01 AM · scripting beginnerinterface

My healthbar and player won't talk to each other! Do we need counseling?

My canvas has a healthbar with a script that controls the slider value (Healthbar.cs) The Event System is in place, the slider script has been dragged into the public function.

My player object has a "stats" script, which defines float values like "health", "healthMax", "ApplyDamage" and so on. Running next to that is "Bar_Control", which (in theory) references those values in the "stats" script and passes them on to the healthbar as slider values. (yes I've dragged the healthbar object into the public variable. Still no dice. Thoughts?

Healthbar:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class HealthBar : MonoBehaviour
 {
     public Slider slider;
 
     public void SetMaxHealth(float health)
     {
         slider.maxValue = health;
         slider.value = health;
     }
 
     public void SetHealth(float health)
     {
         slider.value = health;
     }
 }

Bar_Control:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bar_Control : MonoBehaviour
 {
     float health;
     float healthMax;
     public Modified_Player_Stats statsScript;
 
     public HealthBar healthBar;
 
     public void Start()
     {
         statsScript = GetComponent<Modified_Player_Stats>();
         health = statsScript.health;
         healthMax = statsScript.healthMax;
         healthBar.SetMaxHealth(healthMax);
     }
 
     public void Update()
     {
         healthBar.SetHealth(health);
     }
 }
 

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by streeetwalker · Mar 11, 2020 at 12:08 PM

And you've dragged the actual slider on to your HealthBar script component Slider field? (I think you get a null object error if not, so you must have).

If you put a debug log into SetMaxHealth does it print anything? We need to know if that function is actually getting called. Do the same for SetHealth

You need to find out where the problem is: SetMaxHealth and SetHealth are never getting called? Or SetMaxHealth is only called from the Start of Bar_Control:

For one issue though. This line of code in the Start of Bar_Control

  health = statsScript.health;

only sets the value of health at Start. After that, health is never updated again. Because, if health is a float, you have just made a copy of the player health. It is not a reference to the player health variable.

If you want to make sure player health is the current health in Update, you must include that code there too.

 healthBar.SetHealth(statsScript.health);

C# fundamental variable types are never passed by reference. if you state:

 int a = 1;
 int b = a;
 b++;

We have just made a copy of the value of a. After this code executes a= 1 and b= 2. It doesn't matter if you are referencing such variables in other scripts or not. You are always only copying the value of such variables.

So, unless you are processing health in some way in Bar_Control, as it is now you don't even need that variable. Just access statsScript.health directly.

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

Answer by CerebralStatic · Mar 13, 2020 at 04:53 AM

@streeetwalker Thank you for your input! It worked! For reference, this is Bar_Control now:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Bar_Control : MonoBehaviour
 {
     public Modified_Player_Stats statsScript;
 
     public HealthBar healthBar;
 
     public void Start()
     {
         statsScript = GetComponent<Modified_Player_Stats>();
         healthBar.SetHealth(statsScript.health);
         healthBar.SetMaxHealth(statsScript.healthMax);
     }
 
     public void Update()
     {
         healthBar.SetHealth(statsScript.health);
     }
 }
Comment
Add comment · Show 2 · 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 streeetwalker · Mar 13, 2020 at 03:20 PM 0
Share

@CerebralStatic , glad to hear it worked. Hey, I got this message about your question with respect to not needing Bar_Control at all, and you getting null reference errors. Sorry for the late reply, but I can't seem to find that question/comment referenced in the email notification. Did you solve that problem?

avatar image CerebralStatic streeetwalker · Mar 15, 2020 at 08:33 PM 1
Share

Oh yeah, I tried out multiple solutions and one of them involved tossing out the Bar_Control script altogether. But shortly after that didn't work and I posted the question, I followed your original advice more closely and found that it worked perfectly, thus answering my other question. So ins$$anonymous$$d of leaving the unnecessary question, I changed it into a "Thanks this worked!" response. BTW, more importantly than having a solution, I'm grateful that I now understand what was wrong, what the problem means, what the solution means, and now I can use that knowledge to better understand this space and coding in general.

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

210 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

Related Questions

Unity interface messed up after crash 2 Answers

i am new in unity. how to make a parent child relationship? 1 Answer

How can I change the unity editor completely? 0 Answers

Is it possible to change CustomPass variables via other script? 0 Answers

trying to fade alpha based on the Y angle using a range so every 90 degrees the material changes but fades back n forth 0 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