Created
April 7, 2020 11:50
-
-
Save feranmii/712d2b73347c65b8264a0a772baf2994 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