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}");
}
}
1 replies
Private bool gets changed
C++
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
public class OverlayManager : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private GameObject _pressedObject;
private bool _isHolding;
public float timetoWaitForNewRightClick = 1f;
public DeckManager deckManager;
public DiscardManager discardManager;
private bool _isAreaOverlayOpen = false;
private bool _isOverlayOpen = false;
public void OnPointerDown(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Right)
{
_pressedObject = gameObject;
_isHolding = true;
if (!_isOverlayOpen)
{
StartCoroutine(HoldCheck());
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Right)
{
Debug.LogWarning("Rechtsklick losgelassen"+_isOverlayOpen);
if (_isOverlayOpen)
{
Debug.LogWarning("Schließe Overlay! Und änder Zustand zu false!");
CardOverlayManager.Instance.CloseOverlay();
_isOverlayOpen = false;
return;
}
if (_isAreaOverlayOpen && !gameObject.CompareTag("Card"))
{
_areaOverlayManager.CloseDeckOverlay();
_isAreaOverlayOpen = false;
return;
}
if (_isHolding)
{
_isHolding = false;
if (_pressedObject == gameObject)
{
OpenOverlay();
Debug.LogWarning(_isOverlayOpen);
}
}
}
}
C++
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
public class OverlayManager : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
private GameObject _pressedObject;
private bool _isHolding;
public float timetoWaitForNewRightClick = 1f;
public DeckManager deckManager;
public DiscardManager discardManager;
private bool _isAreaOverlayOpen = false;
private bool _isOverlayOpen = false;
public void OnPointerDown(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Right)
{
_pressedObject = gameObject;
_isHolding = true;
if (!_isOverlayOpen)
{
StartCoroutine(HoldCheck());
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Right)
{
Debug.LogWarning("Rechtsklick losgelassen"+_isOverlayOpen);
if (_isOverlayOpen)
{
Debug.LogWarning("Schließe Overlay! Und änder Zustand zu false!");
CardOverlayManager.Instance.CloseOverlay();
_isOverlayOpen = false;
return;
}
if (_isAreaOverlayOpen && !gameObject.CompareTag("Card"))
{
_areaOverlayManager.CloseDeckOverlay();
_isAreaOverlayOpen = false;
return;
}
if (_isHolding)
{
_isHolding = false;
if (_pressedObject == gameObject)
{
OpenOverlay();
Debug.LogWarning(_isOverlayOpen);
}
}
}
}
5 replies