• 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 Naula · Feb 18, 2013 at 05:26 PM · guitextureguitexthierarchyhealthbardestroy-clones

guitext and texture following gameobject, how to destroy and alternatives?

Okay, i'm trying to create gui system for objects in my scene that follow them around (health shield name background for bars and so on), right now my biggest worries are that hierarchy is getting flooded by guitext and guitexture object clones as i need to instantiate each one for every object so here is my question:

alt text

  1. is there any better way to achieve this?

  2. if not, how can i destroy those clones when the object that created them is destroyed?

i followed this tutorial on youtube and expanded on that.

thanks.

edit: oh yeah i use c# in this.

problem.png (226.5 kB)
Comment
Add comment · Show 4
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 surgesus · Mar 03, 2013 at 03:18 AM 1
Share

Having the same problem, please help. I can not find anything. After the gameobject is destroyed the clone remains., Here is my Java script for it. Thank you for your time.

 var CurrentHealth:float;
 var $$anonymous$$axHealth:float;
 var healthbarWidth:int;
 var myHealthbar:GameObject;
 var myhb:GameObject;
 var target : Transform; 
 
 
 
 function Start()
         {
 healthbarWidth=20;
 myhb=Instantiate(myHealthbar,transform.position,transform.rotation);
 }
 
 function Update (){
     myhb.transform.position=Camera.main.WorldToViewportPoint (transform.position);
     myhb.transform.position.x-=.00;
     myhb.transform.position.y-=.00;
     myhb.transform.localScale=Vector3.zero;
     var healthpercent:float=CurrentHealth/$$anonymous$$axHealth;
     if(healthpercent<0){healthpercent=0;}
     if(healthpercent>100){healthpercent=100;}
     healthbarWidth=healthpercent*30;
     myhb.guiTexture.pixelInset=Rect(10,40,healthbarWidth,5);
 }
 
 
 function ApplyDamage (Damage : float) {
 
 
     if ( CurrentHealth < 0){
     
         
     return;
     
     }
     
     CurrentHealth -= Damage;
     
     if(CurrentHealth <= 0){
    Destroy(transform.root.gameObject);
 }
 }
avatar image robertbu · Mar 03, 2013 at 03:36 AM 0
Share

One way is to keep a game object variable and destroy it in OnDestory()

 function OnDestroy () {
     Destroy(myhb);
 }
avatar image surgesus · Mar 03, 2013 at 04:10 AM 0
Share

@ robertbu That dose kill the extra clone but dose not do what I need.

The problem I am having is that there are two health bars for one guy. The floating health bar works fine as long as you keep looking at him, he takes damage and he dies. But if you look behind you there is another health bar for the same enemy. Any help with this problem would be greatly appreciated.

alt text

untitled-2.jpg (154.3 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Naula · Mar 03, 2013 at 09:24 AM

For the hierarchy problem, use:

 myhb.transform.parent = this.gameObject.transform;

 

and it should parent the HPbar to the object calling it in hierarchy.

as for the second problem, you need to check if the target is in front or back.

 Camera.main.WorldToViewportPoint(transform.position);

that piece of code gives you XYZ values from wich you can see if the target is in front of you or not, and destroy the bars or set size to 0 or something like that...

this is how im using it is somewhere along like this:

 bool visible = false;
 
 abCookie = Camera.main.WorldToViewportPoint(transform.position);
         //abCookie.x  checks if it is in view vertically
         //abCookie .y checks if it is in view horizontally
         //abCookie .z checks if we are front or behind the target
         //abCookie .z < maxDist just checks if its otherwise in view but if its too far it doesn't create bars
 
 
 if (abCookie.x > 0 && abCookie.x < 1 && abCookie.y > 0 && abCookie.y < 1 && abCookie.z > 0 && abCookie.z < maxDist)
         {
             if (!visible)
             {
                 CreateBars();
             }
             visible = true;
         }
         else
         {
             if (visible)
             {
                 RemoveBars();
             }
             visible = false;
         }

hope it helps.

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 Naula · Mar 03, 2013 at 09:36 AM 0
Share

As a side note, this is quite heavy script if you plan on adding bars like background mana shield or something like that, since this script is attached to enemy your fps can take quite heavy hit if there are multiple enemies (okay this number needs to be quite high for it to have impact but still)

i tried making asteroid belt with bars for the amount of resources they have left and names (granted there were over thousand asteroids) and i lost 20-30 fps... it is on my to do list to make better version of this once i have my other scripts finished, might do tutorial videos on youtube if needed.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Relative GUI positioning and Pixel Inset/Offset conversion 2 Answers

Pairing and GUITexture with a Timer? 1 Answer

guiText before guiTexture 1 Answer

GUIText obsolete 1 Answer

Making GUIText appear and disappear 0 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