- Home /
Question by
tmalhassan · Sep 09, 2017 at 12:11 PM ·
c#gameobjecttextimagefilename
How to display a file name on a text field c#
This sounds simple but i'm new to Unity. i have a gameObject with an image component that displays different images. i want to be able to display the file's name without an extension in a text field. for example... The file's name: (Blue Car.png). Output: Blue Car (in a text field).
How can i achieve that through code?
Comment
you can use something like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class a : $$anonymous$$onoBehaviour {
public Text t1;
public GameObject image;
// Update is called once per frame
void Update () {
t1.text = image.name.ToString ();
} }
Best Answer
Answer by look001 · Sep 09, 2017 at 12:22 PM
This should do the job:
Image image;
public Text text;
public void Start() {
image = GetComponent<Image> ();
text.text = image.sprite.name;
}
Don't forget to add using UnityEngine.UI
to the top
Now apply this code to the gameobject with the image, add a Text to your canvas and drag it in the text field of the script. Good Luck ;)
Another simple thing that i couldn't figure out. Thank you, it worked perfectly. Cheers!