Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ben 39 · Jun 20, 2011 at 10:51 AM · collisionguitextureshow

Show enemy health on collision help.

Im Looking for a way to show a GUI Texture(the enemies health) upon entering a collider, then once the enemy is dead the GUITexture no longer shows.

I managed to achieve the first part on my own, but once I kill the enemy, the enemy is destroyed but the health bar is still showing on the screen.

Any help would be appreciated.

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

3 Replies

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

Answer by Joshua · Jun 20, 2011 at 01:32 PM

Oh god, you're using that horrible tornado twins script aren't you? Info about arrays can be found here. If you'd use array here it would look something like this:

 var health : Texture2D[]; 
 static var lives : int = 9;
 var oldLives : int = 0 //so we know lives changed if lives != oldLives
 
 function Update() {
     if( lives != oldLives ){
         guiTexture.texture = health[lives];
         oldLives = lives;
         if( lives = 0 ){
             Destroy( gameObject.Find( "roboBearCannonMod" );
             Destroy( gameObject ) //destroy the guiText gameObject.
         }
     }
 }

significantly shorter, isn't it? ^.^

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 Ben 39 · Jun 20, 2011 at 01:59 PM 0
Share

Though this has shortened the original health script, I still havent been able to make the health bars of the enemy appear when close to them...have you got any ideas for how to do this?

avatar image Joshua · Jun 20, 2011 at 02:02 PM 0
Share

In your question you said you had already solved that part. Anyway, use a trigger for that.

avatar image
2

Answer by aldonaletto · Jun 20, 2011 at 10:57 AM

Child the GUITexture to the enemy (drag it to the enemy object) - when an object is destroyed, all its children are destroyed too.

Comment
Add comment · Show 6 · 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 Ben 39 · Jun 20, 2011 at 11:13 AM 0
Share

When I child the GUITexture, it not longer appears in the game.

avatar image aldonaletto · Jun 20, 2011 at 11:27 AM 0
Share

Yes, I forgot this: position has a different meaning in GUIText and GUITexture - it's expressed in viewport coordinates, which range from 0 to 1. When childed to a non-GUI object, any movevent takes the GUI object out of view. Let me test an idea - I'll be back soon!

avatar image aldonaletto · Jun 20, 2011 at 12:06 PM 1
Share

Does your GUITexture follow the enemy? If it does, you're probably calculating its position in the enemy's script. If it's the case, calculate the health bar position right after the enemy position is updated - the GUITextue may become out of view in the Editor when childed, but its position will be updated any time the enemy moves when playing.
Another alternative could be to use OnDestroy in the enemy's script:

 var myHealth:GameObject;
 
 function OnDestroy(){
 
   Destroy(myHealth);
 }

This works fine, but you must assign the GUITexture to myHealth in the Inspector. Once assigned, you can create a prefab, select the enemy and the health GUI and drag both to the prefab - but every time a new enemy prefab is instantianted you will have to adjust the health bar coordinates, since its position will be initially set to the enemy position.

avatar image Joshua · Jun 20, 2011 at 01:20 PM 0
Share

@aldonaletto It's good practice to edit your main answer if you suggest a totally different approach in a comment then you did in the answer. Upvoted anyway for correctness ;)

avatar image Ben 39 · Jun 20, 2011 at 01:25 PM 0
Share

But I still havent fixed the problem =[

Show more comments
avatar image
0

Answer by Ben 39 · Jun 20, 2011 at 12:57 PM

This is the health code I use for the enemy..its attached to a GUITexture.

 var health8 : Texture2D; //eight lives left
 var health7 : Texture2D; //seven lives left
 var health6 : Texture2D; //six lives left
 var health5 : Texture2D; //five lives left
 var health4 : Texture2D; //four lives left
 var health3 : Texture2D; //three lives left
 var health2 : Texture2D; //two lives left
 var health1 : Texture2D; //one lives left
 var health0 : Texture2D; //no lives left

 static var LIVES1 = 9;

 function Update () 
 {
  switch(LIVES1)
  {
      case 9:
          guiTexture.texture = health8;
          break;
          
      case 8:
          guiTexture.texture = health7;
          break;    
          
      case 7:
          guiTexture.texture = health6;
          break;
          
      case 6:
          guiTexture.texture = health5;
          break;
          
      case 5:
          guiTexture.texture = health4;
          break;
          
      case 4:
          guiTexture.texture = health3;
          break;     
          
      case 3:
          guiTexture.texture = health2;
          break;    
          
      case 2:
          guiTexture.texture = health1;
          break;
          
      case 1:
          guiTexture.texture = health0;
          Destroy(gameObject.Find("roboBearCannonMod"));  // Destroys Health Box
          LIVES1 = 9;
          break;
  }

}

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 stfx · Jun 20, 2011 at 01:05 PM 0
Share

oh god, why dont you use an array? that would drastically reduce the amount of written lines, also you dont need to have a seperate variable for health count as it would be the array size :)

avatar image Ben 39 · Jun 20, 2011 at 01:11 PM 0
Share

How would I create an array?

avatar image Ben 39 · Jun 20, 2011 at 01:30 PM 0
Share

Thank you very much =]

avatar image Joshua · Jun 20, 2011 at 01:33 PM 0
Share

Never$$anonymous$$d, deleted to comment and posted an answer that also solved your main problem. In the future, please don't post answers to your own question like this, but rather edit your main question. It's easier for us to then find the info we need to help you, and it doesn't make it seem like the question is already answered. ;)

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

Show GUI texture on collison 1 Answer

Showing a picture on collision errors 1 Answer

Why are all my GUITexture elements semi-transparent? 0 Answers

How to display textures that are in an array? 1 Answer

How to make an object enter a gui screen upon a collision? 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