Button wont get a listener
private void AttachButtonToCard(GameObject cardObj, GameObject cardButtonPrefab)
{
GameObject buttonObj = Instantiate(cardButtonPrefab, cardObj.transform);
buttonObj.transform.SetParent(cardObj.transform, false);
buttonObj.transform.SetAsLastSibling();
Button buttonComponent = buttonObj.GetComponent<Button>();
if (buttonComponent != null)
{
Debug.Log($"Button added for: {cardObj.name}");
Button buttonCheck = buttonObj.GetComponent<Button>();
Debug.LogWarning($"Button still there? {buttonCheck != null}");
buttonComponent.onClick.RemoveAllListeners();
buttonComponent.onClick.AddListener(() => OnButtonClick(buttonObj));
Debug.Log($"Number of listerners {cardObj.name}: {buttonComponent.onClick.GetPersistentEventCount()}");
Button buttonCheck2 = buttonObj.GetComponent<Button>();
Debug.LogWarning($"Button still there? {buttonCheck2 != null}");
}
}
private void AttachButtonToCard(GameObject cardObj, GameObject cardButtonPrefab)
{
GameObject buttonObj = Instantiate(cardButtonPrefab, cardObj.transform);
buttonObj.transform.SetParent(cardObj.transform, false);
buttonObj.transform.SetAsLastSibling();
Button buttonComponent = buttonObj.GetComponent<Button>();
if (buttonComponent != null)
{
Debug.Log($"Button added for: {cardObj.name}");
Button buttonCheck = buttonObj.GetComponent<Button>();
Debug.LogWarning($"Button still there? {buttonCheck != null}");
buttonComponent.onClick.RemoveAllListeners();
buttonComponent.onClick.AddListener(() => OnButtonClick(buttonObj));
Debug.Log($"Number of listerners {cardObj.name}: {buttonComponent.onClick.GetPersistentEventCount()}");
Button buttonCheck2 = buttonObj.GetComponent<Button>();
Debug.LogWarning($"Button still there? {buttonCheck2 != null}");
}
}
0 Replies