EDIT: I solved it! Feel free to ignore. Solution in replies if you’re curious.
Guys, I’m so close. I’m SO close. All I want to do is set a toggle to true when a button is clicked without using on the button’s native OnClick() method.
- The standard TMP buttons don’t work for what I need so I’m trying to control them via script.
- I have a prefab button that I instantiate two dozen times for the scene.
- I can’t for reasons rely on the standard TMP button “selected” status or other related code.
On the prefab, I’ve linked a very simple script just to store the toggle variable that seems like it’s copying onto the cloned buttons correctly:
public class ButtonToggle : MonoBehaviour
{
public bool isSelected;
private void Start()
{
isSelected = false;
}
}
In my game management script, I apply a listener when each button is instantiated (the method is called in a for loop with some other things going on):
public void InstantiateRightsButton()
{
//Create a button
instantiatedRightsButton = Instantiate(rightsButton, new Vector3(0, 0, 0), rightsButton.transform.rotation);
//Put it in the Canvas object as a child
instantiatedRightsButton.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
//Store the button in a list so you can reference it later
rightsButtonList.Add(instantiatedRightsButton);
//Give it a listener
instantiatedRightsButton.GetComponent<Button>().onClick.AddListener(ToggleButtonStatus);
The listener points to a simple ToggleButtonStatus method that half works. The Debug.Log shows as expected on button click. The toggle stays false.
public GameObject buttonToggleManager; //linked in the inspector to my Button Toggle Manager GO and Button Toggle script.
public void ToggleButtonStatus()
{
buttonToggleManager.GetComponent<ButtonToggle>().isSelected = true;
Debug.Log("I have clicked you.");
}
I’m pretty sure I’m missing something stupid.