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 werseburg · Feb 06, 2013 at 12:52 PM · c#scriptingbasicshealthbar

Screen.width ~ HealthBar problem - C#

I've spent over an hour setting up the GUI.Box in C# for a "healthbar" and even though the code compiles, it seems the logic of the script seems to be correct, the "healthbar" wont show up when playtested and the float value healthBarLength shows up as 0 inside the unity editor. I wanted it to work the following way: GUI.Box is 1/2 of the screenwidth, it becomes shorter when you lose health... It shows nothing when you drag. Also, where should I place the Float healthBarLength for the script to work, if I put it just next to

public int curHealth = 100; public float healthBarLength = (curHealth / maxHealth);

It wont compile at all ;/

using UnityEngine;

using System.Collections;

public class healthbar : MonoBehaviour {

 public int maxHealth = 100;
 public int curHealth = 100;
 public float healthBarLength;

 // Use this for initialization
 void Start () {
     OnGui();
 }
 
 // Update is called once per frame
 void Update () {
 }
 void OnGui()
 {
     healthBarLength = ((Screen.width / 2) / (curHealth / maxHealth));
     GUI.Box(new Rect(100, 100, healthBarLength, 20), curHealth + "/" + maxHealth);
         
 }
 }

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
1
Best Answer

Answer by appearance · Feb 06, 2013 at 01:17 PM

You are doing two mistakes here. First, rename the method from

 void OnGui() {}

to

 void OnGUI () {}
 
 

Method name is case-sensitive.

Second, you should not call OnGUI() from within Start() function. OnGUI() will be called automatically/repeatedly by unity.

Comment
Add comment · Show 3 · 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 werseburg · Feb 06, 2013 at 01:27 PM 0
Share

Thanks for the pointer, I fixed the "()", but it had no affect on the GUI display :(

avatar image Wolfram · Feb 06, 2013 at 01:56 PM 1
Share

Not the (), the upper/lowercase of the function name. Your "OnGui" will never be called.

avatar image werseburg · Feb 06, 2013 at 02:17 PM 0
Share

Oh $$anonymous$$y upper case GOD, haven't noticed that, that has been the case all the time, Thanks for the help!!

avatar image
0

Answer by LukeAntConroy · Feb 06, 2013 at 01:43 PM

using UnityEngine; using System.Collections;

public class PlayerGUI : MonoBehaviour {

 public int maxHealth = 100;
 public int curHealth = 100;
 public float healthBarLength;
 

// Use this for initialization void Start () { //OnGUI(); }

// Update is called once per frame void Update () {

 }

void OnGUI() { healthBarLength = ((Screen.width / 2) / (curHealth / maxHealth)); GUI.Box(new Rect(100, 100, healthBarLength, 20), curHealth + "/" + maxHealth);

} }

I ran your code seems to work fine on mine but just to let you know you don't need to have OnGUI in the Start function as OnGUI will update automatically

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 werseburg · Feb 06, 2013 at 01:52 PM 0
Share

I don't know why but the GUI.Box will not appear in the gameplay mode :/, no matter if it is in the Start () function, or alone

avatar image LukeAntConroy · Feb 06, 2013 at 01:55 PM 0
Share

Is the script attached to camera or a game object ?

avatar image werseburg · Feb 06, 2013 at 02:03 PM 0
Share

Yes, I attached in to the firstperson character controller, did not work, so I dragged it on the camera, no difference either

avatar image LukeAntConroy · Feb 06, 2013 at 02:10 PM 0
Share

As Wolfram said do you have it as OnGui if so change it to OnGUI ,either from that I can't see why it is not working.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

What is setter and getters??,What is setter and getters 3 Answers

How to create a Graph in Unity 1 Answer

Does Unity Script syntax change from release to release? If so how much? 4 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