Noob Question on showing name of Object as Text using Object.ToString

Hello everyone, sorry to ask such a basic question but I’m not sure what to do here

I’m using this script Unity - Scripting API: Object.ToString (unity3d.com) to show the name of the game object as text

//This script changes Text to the name of the GameObject with the script attached
//Attach this script to a GameObject
//Attach a Text GameObject in the Inspector (Create>UI>Text)

using UnityEngine;
using UnityEngine.UI;

public class Example : MonoBehaviour
{
    public Text m_Text;

    private void Start()
    {
        //Check that the Text is attached in the Inspector
        if (m_Text != null)
            //Change the Text to show the GameObject's name
            m_Text.text = "GameObject Name : " + gameObject.ToString();
    }
}

“This script changes Text to the name of the GameObject with the script attached”, But it’s only showing up as "GameObject Name : " on the UI and not the name of the object in the inspector

UPDATE:
That was fixed by, changing this line
m_Text.text = "GameObject Name : " + gameObject.ToString();
to this
m_Text.text = gameObject.ToString();

but now it’s showing “(Unityengine.gameobject)” after the text
Imgur: The magic of the Internet
Does anyone know how to Only show the text name of the Object?

Well, look a bit closer you did "GameObject Name : ", In the docs, it does that as well, I assume “Name” is suppose to be the name of the game object.

So for example,

m_Text.text = "GameObject Head: " + gameObject.ToString();

I might be wrong

EDIT: Maybe not, I think i’m wrong brains dead right now lol

1 Like

@Johan_Liebert123

Ah Thanks, that fixes it for that game object, but if I have a lot of game objects and I want to change the text to the name of the one selected without having to have lines in the script for every individual one, do you know how I would modify the script to allow for that? I’m not asking for a complete script just a pointer to something in the scriptingAPI if you or someone else might now

FIXED, just changed
m_Text.text = "GameObject Name : " + gameObject.ToString();
m_Text.text = gameObject.ToString();

but now it’s showing “(Unityengine.gameobject)” after the text
Imgur: The magic of the Internet
Does anyone know how to Only show the text name of the Object?

1 Like

Solved on this thread, Help Wanted - How to show Prefab Name in UI without (UnityEngine.GameObject) showing after it? - Unity Forum