Hello,
I’m trying to turn grey actionButton when action point number if under the necessary points.
So I’ve had an image in the prefab button called Unable:
Then I’ve open ActionButtonUI script.
I’ve had a serializedfield GameObject called unableGameObjectImage
[SerializeField] private GameObject unableGameObjectImage;
I’ve saved and add the gameobject containing grey with a little of alpha squared image in the right field:
then in the ActionButtonUi script I’ve created a new method to manage if button is grey or not:
public void UpdateUnableActionButton()
{
unableGameObjectImage.SetActive(true);
}
I’ve then created a Start method to avoid grey button
private void Start()
{
unableGameObjectImage.SetActive(false);
}
And after I’ve tried many option to update setActive false according to the unit’s bool CanSpendActionPointsToTakeAction but withour any success…
If i created an Update method like that:
test if the unit.CanSpendActionPointsToTakeAction is false then display (SetActive(true)) the unableGameObjectImage but it looks that it isn’t the right way to reach the boolean.
It wants dome baseAction between () and when I put on it baseAction, nothing happens…
private void Update()
{
if (unit.CanSpendActionPointsToTakeAction(baseAction))
{
unableGameObjectImage.SetActive(true);
}
}
I’ven’t error but nothing happens when unit action point fall to zero…
I think I’m not far from the solution but I don’t see what can be wrong…
I precise I’ve created an Unit unit private variable in my Action Button UI script.
Here is the complet one.
Any idea?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//Pour pouvoir utiliser la fonction Button
using TMPro;
public class ActionButtonUI : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI textMeshPro;
[SerializeField] private Button button;
[SerializeField] private GameObject selectedGameObject;
[SerializeField] private GameObject unableGameObjectImage;
private BaseAction baseAction;
private Unit unit;
private void Start()
{
unableGameObjectImage.SetActive(false);
}
private void Update()
{
if (unit.CanSpendActionPointsToTakeAction(baseAction))
{
unableGameObjectImage.SetActive(true);
}
}
public void setBaseAction(BaseAction baseAction)//Methode qui définira les actions possibles par unité, basé sur le script BaseAction qui listera les actions possible en fonction des unités.
{
//On déclare que pour l'unité sélectionnée, l'action sélectionnée est celle sélectionnée.
this.baseAction = baseAction;
textMeshPro.text = baseAction.GetActionName().ToUpper();//Le texte affiché dans le bouton sera celui de l'action en question.
button.image.sprite = baseAction.GetActionIcon();
UpdateUnableActionButton();
//Création de la logique lors du clic sur un des boutons
button.onClick.AddListener(() =>
{
//On déclenche l'action en fonction du bouton cliqué.
UnitActionSystem.Instance.SetSelectedAction(baseAction);
});
}
//Méthode pour mettre à jour le visuel du bouton sélectionné et ceux non sélectionnés
public void UpdateSelectedVisual()
{
//L'action basique sélectionnée est celle sélectionnée par le joueur
BaseAction selectedBaseAction = UnitActionSystem.Instance.GetSelectedAction();
selectedGameObject.SetActive(selectedBaseAction == baseAction);
}
public void UpdateUnableActionButton()
{
unableGameObjectImage.SetActive(true);
}
}
Thanks.
François