{
// Set up button click listeners
buttonCoffee.onClick.AddListener(() => DisplayResult("Coffee"));
buttonIceCoffee.onClick.AddListener(() => DisplayResult("Ice Coffee"));
buttonLatte.onClick.AddListener(() => DisplayResult("Latte"));
buttonIceLatte.onClick.AddListener(() => DisplayResult("Ice Latte"));
void Update()
{
// Check if player is pressing the Fire1 button
if (Input.GetButtonDown("Fire1")) // Fire1 is mapped to Mouse0 by default
{
PointerEventData pointerData = new PointerEventData(EventSystem.current)
{
position = Input.mousePosition // Use mouse position for UI raycasting
};
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, raycastResults);
foreach (RaycastResult result in raycastResults)
{
Button button = result.gameObject.GetComponent<Button>();
// Ensure the hovered UI element is a button
if (button != null && result.gameObject == button.gameObject)
{
button.onClick.Invoke(); // Trigger the button's click event
Debug.Log($"Button {button.name} clicked via Fire1!");
break; // Exit after processing the button
}
}
}
}
public void DisplayResult(string itemName)
{
// If it's the first item added, make the result container visible and hide the Empty_Results text
if (resultContainer.childCount == 0)
{
// Show resultContainer and hide Empty_Results text
resultContainer.gameObject.SetActive(true);
emptyResultsText.gameObject.SetActive(false);
// Make the first child invisible and show Empty_Results text (only for the first child)
if (resultContainer.childCount > 0)
{
resultContainer.GetChild(0).gameObject.SetActive(false); // Hide the first child
}
}
// If the resultContainer has no more room (maxItems reached), don't add more items
if (resultContainer.childCount < maxItems)
{
// Instantiate a new result box from the prefab
GameObject newResult = Instantiate(resultPrefab, resultContainer);
Text resultBoxText = newResult.GetComponentInChildren<Text>();
if (resultBoxText != null)
{
resultBoxText.text = itemName;
}
// Make sure the 0th child stays invisible after the first button press
if (resultContainer.childCount > 0)
{
resultContainer.GetChild(0).gameObject.SetActive(false); // Hide the 0th child
}
// After the first item, keep subsequent items visible
for (int i = 1; i < resultContainer.childCount; i++)
{
resultContainer.GetChild(i).gameObject.SetActive(true); // Make sure the other children are visible
}
}
}
}
}``` ok guys so my problem is i am trying to make a register system for my cafe store simulator game. however lets say i press the button coffee once, the name coffee comes up in results once. but when i press ice coffee once, the result comes up twice. i dont know why it does this. anyone have any ideas what can be causing this? sorry this is my first post not sure if i didnt follow any rules.
#help with button being registered twice.
1 messages · Page 1 of 1 (latest)