C
C#5w ago
RedFox3D

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}");
}
}
produces these logs: - Button added for: Card_Air Boost - Button still there? True - Number of listerners Card_Air Boost: 0 - Button still there? True
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?