-
-
Save jorik041/c6ab2187bbc20b1ffb4ef8456c20d3b5 to your computer and use it in GitHub Desktop.
Simple Class that assigns the name of the parent GameObject to a UI TextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using TMPro; | |
using UnityEngine; | |
/// <summary> | |
/// Simple Class that assigns the name of the parent GameObject to a UI TextField | |
/// </summary> | |
//Unitytips by @theferfactor | |
[ExecuteInEditMode] | |
public class UIGetName : MonoBehaviour | |
{ | |
private void Update() | |
{ | |
var textObject = GetComponent<TextMeshProUGUI>(); | |
var parent = transform.parent; | |
if (textObject != null && parent != null) | |
{ | |
textObject.text = parent.name; | |
textObject.gameObject.name = $"{parent.name} - Text"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment