Skip to content

Instantly share code, notes, and snippets.

@DryreL
Created October 20, 2024 14:34
Show Gist options
  • Save DryreL/f9f40d288d85910ff12a78858f3ba494 to your computer and use it in GitHub Desktop.
Save DryreL/f9f40d288d85910ff12a78858f3ba494 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode] // Allows the script to run in the editor
[RequireComponent(typeof(Button))] // Ensure the GameObject has a Button component
public class ButtonTextUpdater : MonoBehaviour
{
public Button myButton; // Reference to the Button component
public string buttonText = "Default Text"; // Default button text
// Called whenever a value is changed in the Inspector
void OnValidate()
{
// Automatically assign the Button component if not set
if (myButton == null)
{
myButton = GetComponent<Button>();
}
// Update the button text if the Button component is available
if (myButton != null)
{
// Update the button text without having to press Play
myButton.GetComponentInChildren<Text>().text = buttonText;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment