Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by eeveelution8 · Apr 03, 2014 at 01:56 AM · health barhealth

health bar referencing another scripts variable

This is the health script that Im using.

 var health : playerhealth = gameObject.GetComponent(playerhealth);
 
 
 
 
 var initialGreenLength: float;
 var greenBar: GameObject;
 var helth: int = health.HP;
 var maxHealth: float=100;
  
 function Awake(){
 helth=maxHealth;
 initialGreenLength=greenBar.transform.localScale.x;
 }
  
 function Update(){
 greenBar.transform.localScale.x=initialGreenLength*(helth/maxHealth);
  
 }


The

var health : playerhealth = gameObject.GetComponent(playerhealth);

section of the script references another scripts variable of player HP, but it doesn't seem to be working. This is the other script

 var HP : int = 5;
 var body = gameObject;
 
 function Update () {
 
     if(HP < 1) {
         
         var instance : GameObject = Instantiate(body, transform.position, transform.rotation);
         DestroyObject (gameObject);
     }
 }


How do I fix it so it works?

Comment
Add comment · Show 3
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 Scribe · Apr 03, 2014 at 03:13 AM 0
Share

Whats currently going wrong? Do you have an error message or what is not happening that you think should be.

avatar image FirePlantGames · Apr 03, 2014 at 03:18 AM 0
Share

you spelled health wrong ;)

avatar image eeveelution8 · Apr 03, 2014 at 06:35 AM 0
Share

It was spelled helth because health was already a variable

3 Replies

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

Answer by eeveelution8 · Apr 03, 2014 at 07:42 PM

I figured out how to fix it, I deleted the HP bar script, made a GUIText, and changed the player health script to look like this,

 var gui : GUIText = gameObject.GetComponent(GUIText);
 var HP : int = 5;
 var body = gameObject;
 
 function Update () {
     
     gui.text = "Health: "+HP;
     
     if(HP < 1) {
         
         var instance : GameObject = Instantiate(body, transform.position, transform.rotation);
         DestroyObject (gameObject);
     }
 }


thanks everyone for the help though.

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
1

Answer by FirePlantGames · Apr 03, 2014 at 03:25 AM

var health : playerhealth = gameObject.GetComponent(playerhealth);

should be: var health : int = gameObject.GetComponent(playerhealth).HP;

If I understand you correctly, also "playerhealth" needs to be the 'destroy' scripts name

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 eeveelution8 · Apr 03, 2014 at 07:26 PM 0
Share

I tried what you suggested, but it comes up with the error,

transform.localScale assign attempt for 'healthbar' is not valid. Input localScale is { NaN, 1.000000, 0.005000 }. UnityEngine.Transform:set_localScale(Vector3) hp counter:Update() (at Assets/scripts/hp counter.js:17)

avatar image
0

Answer by alti · Jun 25, 2014 at 06:30 AM

This probably happened because Helth or MaxHealth was == 0. One rule in math: You cannot divide by 0. If you do something like:

(Helth + 0.0001)/(MaxHealth + 0.0001); You would be good (add the 0.001 to whatever could potentially be 0).

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

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

23 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

Related Questions

Can someone write me a health script? 2 1 Answer

How to Make Health Decrease Over Time 2 Answers

Enemy Health Help 1 Answer

Why don't my send messages work? 2 Answers

Enemy Health Problems 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