- Home /
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:
is there any better way to achieve this?
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.
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);
}
}
One way is to keep a game object variable and destroy it in OnDestory()
function OnDestroy () {
Destroy(myhb);
}
@ 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.
I'm confused, but I'm going to make a guess here. I think this is the same health bar. I only see one Instantiate(), but since GUI uses screen coordinates and displays on top of everything. You see it even when you turn away. If so, one way of fixing the problem is to use the Renderer.isVisible flag. When the enemy is not visible, then you can turn the bar off. Let me know if that is the problem or not.
For future reference, it keeps the questions cleaner and you'll get better results if you post as a new question with a link reference to the old. Because you posted as a answer, this question came across as already having an answer, so it will get less attention. Plus it looks like the OP may have had a very different problem.
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.
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.