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 Azimuth · Apr 26, 2012 at 01:54 AM · javascriptguihealth

Can't move GUI

I'm trying to get a GUI health bar in my 2-player game. After reading through many similar questions on this site, I still can't find a way to resolve this issue.

I was looking on the Unity GUI Scripting Guide to find how to make a health bar, and it gives this code:

     var bgImage : Texture2D; // background image that is 256 x 32
     var fgImage : Texture2D; // foreground image that is 256 x 32
     var playerEnergy = 1.0; // a float between 0.0 and 1.0
     function OnGUI () {
 // Create one Group to contain both images
 // Adjust the first 2 coordinates to place it somewhere else on-screen
 GUI.BeginGroup (Rect (0,0,256,32));

 // Draw the background image
 GUI.Box (Rect (0,0,256,32), bgImage);

 // Create a second Group which will be clipped
 // We want to clip the image and not scale it, which is why we need the second Group
 GUI.BeginGroup (Rect (0,0,playerEnergy * 256, 32));

 // Draw the foreground image
 GUI.Box (Rect (0,0,256,32), fgImage);

 // End both Groups
 GUI.EndGroup ();
 GUI.EndGroup ();
 }

I'm assuming this works just fine if it were singleplayer, but that is not what I want. So since I have splitscreen, I thought I should make 2, and move one of them to the other side of the screen. This doesn't work, as whenever I try to move it, the box just splits the background and foreground and/or stretches all over the place and doesn't work (see image)

This is the code I wrote that i thought would let me be able to adjust its' position:

 var bgi : Texture2D;
 var fgi : Texture2D;
 var hitCounterCheck : int;
 var otherScript : TankScript;
 var otherObject : GameObject;
 var posW : int;
 var posH : int;
 
 function Awake (){
 
 otherObject = gameObject.Find("Player 1");
 otherScript = otherObject.GetComponent(TankScript);
 
 }
 function Update () {
 hitCounterCheck = otherScript.hitCounter;
 }

 function OnGUI(){
 
 GUI.BeginGroup (Rect (posW,posH,posW+256,posH+20));
 GUI.Box (Rect (posW,posH,posW+256,posH+20), bgi);
 GUI.EndGroup();
 GUI.BeginGroup (Rect (posW,posH,hitCounterCheck * 51+posW,posH+20));
 GUI.Box (Rect (posW,posH,posW+256,posH+20), fgi);
 GUI.EndGroup();
 
 }

But that just causes the image below, where the first group stays at 0,0, and refuses to move. How can I get them both to move where I need?

alt text

unityhalp.jpg (207.9 kB)
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

· Add your reply
  • Sort: 
avatar image
0

Answer by Calumcj · May 10, 2012 at 09:25 AM

Hey, im still new to this but i will have a go :) You are setting your possisions with a in game edit fromt he inspecter panel. what would could try doing witht he possisioning is setting the co-ordanets in the script. e.g to make a box

function OnGUI () {

GUI.box(Rect ( 10, 30, 100, 50)"") {

}

}

This seems to work for me, so it might be worth a go :) (you can text in the box in the "" :))

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 syclamoth · May 10, 2012 at 09:50 AM 0
Share

Please, format your code properly. It makes things much more readable.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Please help me make a health system with GUI. I cant do it! 3 Answers

Health bar only updates on character death 1 Answer

LoadLevel Health Question. 1 Answer

Unity freezes when changing GUI.Color.a 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