Search Unity

Text File to Unity 3D GUI Canvas Textbox

Discussion in 'Scripting' started by RDG_Admin, Aug 13, 2019.

  1. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    Newbie to Code here... So I was given this code by a user who claimed this would do what I want it to do... which is display the txt file noted in the code and post the single line information in it to a GUI Textbox...

    However, nothing happens - any ideas what's wrong?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System.IO;

    public class driverVersionText : MonoBehaviour
    {

    public Text driverVerText = null;
    // Start is called before the first frame update
    void Start()
    {
    driverVerText.text = System.IO.File.ReadAllLines(@"C:\DriverVersion.txt");
    }
    }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    The code itself is correct. The problem must be in the scene setup. Have you created a canvas and a text label for the script? Have you checked file content fits label before running the script? Check console for any errors. Copy and paste file content to text label inspector to see if it is displayed correctly at all.
     
  3. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    upload_2019-8-13_13-29-55.png

    That's the console... noooo idea lol
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You splitted text into array of lines, but the component expect all text as a whole. Use ReadAllText instead of ReadAllLines
     
  5. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    Problem solved! Thanks for your help :)