Author Topic: Health bar foreground always starts enabled  (Read 7221 times)

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Health bar foreground always starts enabled
« on: July 26, 2012, 06:37:21 PM »
Hi,

I've followed the health bar instructions for creating floating healthbars above characters and it works great for the most part.

The only problem I am running into is that even when all the characters are off screen during the start of a scene, the foreground element of the progress bar of the health bar always becomes enabled. Stepping through the script I can see that the first time the LateUpdate is run on the health bar script it correctly disables both the foreground and the background of the healthbar progress bar, but then for some reason the foreground element re-enables somewhere else. So the result is that at the start of the level all the health bar foregrounds are shown at the center of the screen even if no characters are within view. Once the characters are visible the healthbar foreground appears correctly with the rest of the healthbar above the characters.

Is the UI Slider maybe changing the foreground when it activates? and thereby enabling the foreground uiwidget?

I can explain in more detail and some some code if that would help.

-JJ

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #1 on: July 27, 2012, 05:56:44 AM »
My guess is that everything gets disabled correctly, but the "change" event is not received or handled correctly by the panel, leaving old geometry visible. Make sure that the panel you're using to display your health bars isn't marked as "static" on the UIPanel (not on the game object).

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #2 on: July 28, 2012, 05:37:13 PM »
I checked the static box but it's not selected, either on the gameobject or the panel that the health bars get attached to. Also, when I look at the foreground and background objects, the foreground sliced sprite is enabled and the background sliced sprite is disabled, even though both become disabled when that lateupdate runs.

If I manually disable the foreground slicedsprite it stays disabled and works correctly from then on.

-JJ

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #3 on: July 28, 2012, 05:39:36 PM »
I'd suggest adding a debug statement inside OnEnable then, to see where it's getting enabled from.

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #4 on: July 28, 2012, 05:48:28 PM »
Good idea. Here's the debug from the health bar's widget getting enabled:

UnityEngine.Debug:Log(Object)
UIWidget:OnEnable() (at Assets/NGUI/Scripts/Internal/UIWidget.cs:239)
UnityEngine.Behaviour:set_enabled(Boolean)
UISlider:Set(Single, Boolean) (at Assets/NGUI/Scripts/Interaction/UISlider.cs:254)
UISlider:Start() (at Assets/NGUI/Scripts/Interaction/UISlider.cs:136)

Looks like like 254 in the slider is enabling the widget?
-JJ

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #5 on: July 28, 2012, 05:53:45 PM »
Your line numbers don't match mine. Make sure you update to the latest. Assuming you're talking about line 262:
  1. mFGWidget.enabled = true;
You should be disabling game objects, not UI components. Use NGUITools.SetActive(sliderGameObject, false); instead of whatever you're doing.

capitalj

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 88
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #6 on: July 28, 2012, 05:57:13 PM »
Ah yes, that is the correct line. I'm going to have to update.

In terms of disabling the widgets, I just used the health bar code which disables the actual widgets not the game object, should I change this to use the SetActive and disable the entire healthbar or the gameobject containing the sliced sprite?

  1. void LateUpdate()
  2.         {
  3.                 if (target == null) { Destroy(gameObject); return; }
  4.  
  5.                 mPos = mGameCam.WorldToViewportPoint(target.position);
  6.  
  7.                 bool visible = (mPos.z > 0f && mPos.x > 0f && mPos.x < 1f && mPos.y > 0f && mPos.y < 1f);
  8.  
  9.                 // only show health bar once ship has been hit
  10.                 if( targetShip.CurrentHP == targetShip.MaxHP )
  11.                         visible = false;
  12.                
  13.                 if (mVisible != visible)
  14.                 {
  15.                         mVisible = visible;
  16.                         UIWidget[] widgets = gameObject.GetComponentsInChildren<UIWidget>();
  17.                         foreach (UIWidget w in widgets)
  18.                                 w.enabled = mVisible;
  19.  
  20.                 }
  21.  
  22.                 if (mVisible)
  23.                 {
  24.                         mPos = mUICam.ViewportToWorldPoint(mPos);
  25.                         mPos.z = 0f;
  26.                         mPos.y += 0.1f;
  27.                         mTrans.position = mPos;
  28.                         if( targetShip.CurrentHP > 0 )
  29.                                 healthBarSlider.sliderValue = (float)targetShip.CurrentHP / (float)targetShip.MaxHP;
  30.                         else
  31.                                 Destroy(gameObject);
  32.                 }
  33.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Health bar foreground always starts enabled
« Reply #7 on: July 28, 2012, 06:02:20 PM »
Yup. What I did in Windward, is each Unit HUD element has a script (coincidently called UnitHUD) that enables/disables all of its children when the object itself enters or leaves the view. Health bar is just one of those elements. HUDText is another.