Skip to content

Instantly share code, notes, and snippets.

@jorik041
Forked from feranmii/UIGetName.cs
Created April 7, 2020 18:19
Show Gist options
  • Save jorik041/c6ab2187bbc20b1ffb4ef8456c20d3b5 to your computer and use it in GitHub Desktop.
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
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