• 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 krisjhamilton · Jun 05, 2012 at 02:28 PM · guihealthrtsonmouseovertower

OnMouseOver Tower Gui Health Bar

Hey everyone,

I have found unityanswers so helpful, but I have finally hit a brickwall and therfore this is my first post ever...

Like a lot of people, I am trying to create a TD / RTS. The first script below is to show a GUI when mouse is over the spawned "Enemy". This script works perfectly as it only displays one Texture. The second script below is attached to the spawned "Player". The issue im having is I have tried to implement a health system, that being that every time the health decreases on the "Player" by 1, the Texture changes to the Texture relating to the health amount. Currently it is at a point where the script will run, but the mouseover function is not working!!! Nothing appears, nor are there any errors at runtime... I would appreciate any help on this!!!


Enemy

 var healthTex : Texture2D;
 var health : int = 40;
 var target : Transform;
 
 private var _mouseOver = false;
 
 function OnGUI()
 {
     if(!_mouseOver) return;
         var screenPos : Vector3 = Camera.main.WorldToScreenPoint(target.position);
         GUI.DrawTexture(Rect(screenPos.x - 25.0f,Screen.height - screenPos.y-50.0f,40,12), healthTex);
 
 }
 function OnMouseOver()
 {
     _mouseOver = true;
 }
 function OnMouseExit()
 {
     _mouseOver = false;
 }


Player

 var health : int = 5;
 var target : Transform;
 
 var h1  : Texture2D;
 var h2  : Texture2D;
 var h3  : Texture2D;
 var h4  : Texture2D;
 var h5  : Texture2D; 
 
 var healthTex : Texture2D = h5;
 
 private var _mouseOver = false;
 
 function OnUpdate()
 {
     
     if (health == 5)
     {
         healthTex = h5;
     }
     else if (health == 4)
     {
         healthTex = h4;
     }
     else if (health == 3)
     {
         healthTex = h3;
     }
     else if (health == 2)
     {
         healthTex = h2;
     }
     else if (health == 1)
     {
         healthTex = h1;
     }
 
 //Enter health = 0 dah dah dah...
 
 }
 
 function OnGui()
 {
     if(!_mouseOver) return;
         var screenPos : Vector3 = Camera.main.WorldToScreenPoint(target.position);
         GUI.Box(Rect(screenPos.x - 25, Screen.height - screenPos.y, 40, 12), healthTex);
 }    
     
 
 function OnMouseOver()
 {
     _mouseOver = true;
 }
 function OnMouseExit()
 {
     _mouseOver = false;
 }



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 phren · Jun 05, 2012 at 04:08 PM

I think the problem here might be that your OnUpdate function doesn't get called. I think you mean to use Update().

If changing function names does not solve the problem you should check whether OnMouseOver gets called at all by adding Debug.Log into it. If it gets called the raycast probably just ignores the player and not the monsters because of some layer issue.

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 krisjhamilton · Jun 06, 2012 at 12:32 PM

You are absolutely right!!! Also, function OnGui should be OnGUI... its very easy to miss things when you are constantly staring at code haha.

Thank you very much!

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 phren · Jun 06, 2012 at 01:23 PM 0
Share

I know the feeling. :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RTS Unit Creation Bar Queue System 1 Answer

How to prevent guitexture from vertically shrinking? 0 Answers

How to display gui.texture from a different script 1 Answer

GUI SCREW UPS! 1 Answer

Deduct health on collision 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges