I have a strange bug that I haven’t been able to solve where after it goes back to the player’s turn, the action buttons will all be disabled. They won’t work until a new unit is chosen and then it’s fine. I currently have it where when Action Points are zero then the buttons are disabled. It seems like it is some problem with how events are ordered but I can’t seem to figure it out. Here’s what I have right now in UnitActionSystemUI.cs
private void UpdateSelectedVisual()
{
foreach (ActionButtonUI actionButtonUI in actionButtonUIList)
{
actionButtonUI.UpdateSelectedVisual();
}
}
private void TurnSystem_OnTurnChanged(object sender, EventArgs e)
{
UpdateActionPoints();
UpdateSelectedVisual();
//CreateUnitActionButtons(); //Fixes not working on change but makes things work that shouldn't
}
private void CreateUnitActionButtons()
{
foreach (Transform buttonTransform in actionButtonContainerTransform) //Clean up by destroying all buttons
{
Destroy(buttonTransform.gameObject);
}
actionButtonUIList.Clear(); //Make sure nothing is highlighted initially
Unit selectedUnit = UnitActionSystem.Instance.GetSelectedUnit();
foreach (BaseAction baseAction in selectedUnit.GetBaseActionArray())
{
if (baseAction.AbilityAssigned() == true)
{
Transform actionButtonTransform = Instantiate(actionButtonPrefab, actionButtonContainerTransform); //Instantiate prefab in container
ActionButtonUI actionButtonUI = actionButtonTransform.GetComponent<ActionButtonUI>(); //Grab transform for use
actionButtonUI.SetBaseAction(baseAction); //Call function
actionButtonUIList.Add(actionButtonUI);
}
}
}
In ActionButtonUI.cs
public void UpdateSelectedVisual()
{
BaseAction selectedBaseAction = UnitActionSystem.Instance.GetSelectedAction();
selectedGameObject.SetActive(selectedBaseAction == baseAction); //Set active if selected matches this one
bool canPerform = button.interactable = baseAction.CanPerformAction();
if (canPerform)
{
Unit unit = UnitActionSystem.Instance.GetSelectedUnit();
button.interactable = unit.CanSpendActionPointsToTakeAction(baseAction);
}
}
I also tried a method where private void UnitActionSystem_OnSelectedUnitChanged(object sender, EventArgs e)
was triggered in UnitActionSystem.cs
whenever it became the player’s turn again, but that still didn’t work.