Skip to content

Instantly share code, notes, and snippets.

@feranmii
Created April 7, 2020 11:50
Show Gist options
  • Save feranmii/712d2b73347c65b8264a0a772baf2994 to your computer and use it in GitHub Desktop.
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
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