Action points not updating after pressing play

Hello,

I am not sure why the world UI action points are not getting updated after I press play, they update correctly for all the units only after I spend the first point.

public class UnitWorldUI : MonoBehaviour
{
   [SerializeField] TextMeshProUGUI _actionPointsText;
   [SerializeField] Unit _unit;


   void Start()
   {
      Unit.OnAnyActionPointsChanged += Unit_OnAnyActionPointsChanged;
      UpdateActionPointsText();
   }
   void Unit_OnAnyActionPointsChanged(object sender, EventArgs e)
   {
      Unit unit = sender as Unit;
      Debug.Log($"{unit}'s action points have changed.");
      UpdateActionPointsText();
   }

   void UpdateActionPointsText()
   {
      _actionPointsText.text = _unit.GetActionPoints().ToString();
      Debug.Log("Points have been updated");
   }
}

The debug does, however, show up in the console.

What you are experiencing is a race condition. Your UnitWorldUI’s Start() is firing before the unit is initializing it’s ActionPoints.

Most likely, you’re initializing the Action Points the first time (likely in Start()) without calling OnAnyActionPointsChanged.

2 Likes

Indeed! I added a 0.1 second coroutine in the start method and it solved my issue. Thank you for the answer! :slight_smile:

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms