Using the following code using UnityEngine; using System; using UnityEngine.UI; using…
- User
Using the following code using UnityEngine; using System; using UnityEngine.UI; using TMPro;
public class dev_InteractCollider : MonoBehaviour { public dev_InteractButton interactableButton;
public void Awake() { interactableButton.gameObject.SetActive(false); } void OnTriggerEnter(Collider other) { Button interactBtn = interactableButton.GetComponent<Button>(); TMP_Text interactBtnText = interactableButton.buttonText.GetComponent<TMP_Text>(); if (other.CompareTag("Player")) { interactBtnText.text = "Buy Small Barn - $2500"; interactableButton.gameObject.SetActive(true); interactBtn.onClick.AddListener(BuyBarnTutorial); } } void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { interactableButton.gameObject.SetActive(false); } } void BuyBarnTutorial() { Destroy(this.gameObject); interactableButton.gameObject.SetActive(false); }} My button won't change text
- Assistant
It's likely that the issue is with the references to your Button component and Text component. Check if the
interactableButtonandinteractableButton.buttonTextreferences are properly assigned in the Inspector. If the references are correct, please replace the code insideOnTriggerEnterfunction with the following code:void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { interactableButton.buttonText.text = "Buy Small Barn - $2500"; interactableButton.gameObject.SetActive(true); interactableButton.GetComponent<Button>().onClick.AddListener(BuyBarnTutorial); } }This slight modification accesses the
TMP_Textcomponent directly through theinteractableButton.buttonTextreference instead of using a separate variable.If the issue still persists, please ensure there are no errors or warnings in the Unity console.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.