Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: kitgui on December 09, 2013, 08:48:06 PM

Title: Bug with HP Bars
Post by: kitgui on December 09, 2013, 08:48:06 PM
Since a few versions ago (I think 3.0.6f7) my HP Bars have started having some strange bugs.

Some context to the problem :

My player has a HP Bar anchored to the top-left of the screen. I also give every Enemy in the game a HP Bar(instantiated in code), and make the Bar invisible(via SetActive(false)) in Initialize. When an enemy takes damage for the first time (and so has 100%< HP), I make his HP Bar visible (by doing SetActive(true)).

As soon as I set an Enemy's HP Bar to Active, the player's HP Bar snaps to the center of the screen, as if gaining an offset from the top left. This lasts for 1 frame, and then it snaps back to normal. This happens every single time I set an enemy's HP Bar to Active

(http://i40.tinypic.com/2iqcu3a.png)
(http://i42.tinypic.com/ano2oo.png)

I have looked through my code, and there is code that moves the HP Bars of enemies, but no other UI elements have code that change their positions.

The NGUI hierarchy looks like this :

UIRoot (My code here for instantiating player HUD and enemy HP Bars)
-Camera
--Anchor
---Enemy HP Bars
---Players (UIPanel)
----Player 1 (My code here to manage HP Bars and other elements)
----- HP Bar


Please let me know if you require more details
Title: Re: Bug with HP Bars
Post by: ArenMook on December 10, 2013, 01:01:09 AM
The logic that moves the bar likely kicks in a frame after. What are you using to reposition the bars? Just call that function after activating the object.
Title: Re: Bug with HP Bars
Post by: kitgui on December 10, 2013, 04:13:44 AM
Hi ArenMook, thanks for your reply.

I don't have any logic that moves Player 1's HP Bar, its default position is set in a Prefab. When I hit a Rat for the first time, I call SetActive(true) on the HP Bar linked to that rat, and that seems to make Player 1's HP Bar (the big one) snap to the center.

Then in the next frame, Player 1's HP Bar snaps back, without me moving it. I hope this made things a bit clearer, thanks.
Title: Re: Bug with HP Bars
Post by: kitgui on December 10, 2013, 05:53:58 PM
I tried toggling 'enable' on UISprite instead of toggling 'Active' on the HP Bar's gameobject, the snapping persists.

So it looks as if UISprite does something when it is enabled? Maybe trying to find the right GameObject to parent itself to?
Title: Re: Bug with HP Bars
Post by: ArenMook on December 10, 2013, 09:30:24 PM
It does look for a panel, yes. You need to use NGUITools.AddChild(parent, prefab) to instantiate your object so that it ends up in the right place. Also when setting active, use NGUITools.SetActive.
Title: Re: Bug with HP Bars
Post by: kitgui on December 10, 2013, 11:10:40 PM
Thanks for your reply, it helped me solve the problem :)

I was originally parenting my Enemy HP Bars to the Anchor gameobject(refer to my hierarchy above), because I thought that was a logical place for them to be. But the Anchor wasn't a UIPanel.

I eventually tried attaching a UIPanel script to the Anchor, because I guessed that they were parenting themselves to the 'Players' UIPanel, and for some reason that was changing it's transform.

I guess the moral of the story is that you need to have UIPanel on your parent object.