Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Read text from file and show it on GUI

Discussion in 'Scripting' started by tarzanno, May 29, 2015.

  1. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I want to make a script that reads random line from a .txt file and shows it on a screen. I found some solutions, but not for Unity, but as a C# standard.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine.UI;
    5.  
    6. public class TextReadFromFile : MonoBehaviour {
    7.  
    8.     public TextReader quotes;
    9.     Text text;
    10.     public string textFromFile;
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         text = GetComponent <Text>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         text.text = "Sentence::: " + textFromFile;
    21.           }
    22.     private void ReadFromFile()
    23.     {
    24.         string[] allLines = File.ReadAllLines("quotes.txt");
    25.         Random rnd1 = new Random();
    26.         return allLines[rnd1.Next(allLines.Length)];
    27.         textFromFile = allLines;
    28.  
    29.     }
    30. }
    There are multiple errors, but first of all, there is a problem with "return allLines[rnd1.Next(allLines.Length)];".
    Have anybody had the same problem?
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    the UnityEngine version of Random doesn't have a Next function. If you want the system one, you'll need to change it to 'System.Random'
     
  3. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Thanks for that.
    Can you tell me, where is the Unity looking for my file? Also, what should be the type of my textFromFile file in code?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine.UI;
    5.  
    6. public class TextReadFromFile : MonoBehaviour {
    7.  
    8.     public TextReader quotes;
    9.     Text text;
    10.     public string textFromFile;          //there should be something else
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         text = GetComponent <Text>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         text.text = "Napis::: " + textFromFile;
    21.         }
    22.  
    23.  
    24.     private void ReadFromFile()
    25.     {
    26.         string[] allLines = File.ReadAllLines("quotes.txt");
    27.         System.Random rnd1 = new System.Random();
    28.               textFromFile = allLines[rnd1.Next(allLines.Length)];
    29.  
    30.      }
    31.     }
     
  4. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    Unity looks in ...
    Code (CSharp):
    1. Resources.Load("filenamehere");
    That's assuming you have the file in your resources folder.
     
  5. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I have this error (one of many):
    Cannot implicitly convert type `UnityEngine.Object' to `System.IO.StringReader'
    on "StringReader allLines = Resources.Load ("quotes.txt");"

    How should I convert type Object to string[]?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine.UI;
    5.  
    6. public class TextReadFromFile : MonoBehaviour {
    7.  
    8.  
    9.     Text text;
    10.     public string textFromFile;
    11.  
    12.     void Awake()
    13.     {
    14.         text = GetComponent <Text>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         text.text = "Napis::: " + textFromFile;
    20.  
    21.     }
    22.  
    23.  
    24.     private void ReadFromFile()
    25.     {
    26.  
    27.  
    28.        StringReader allLines = Resources.Load ("quotes.txt");
    29.  
    30.  
    31.         System.Random rnd1 = new System.Random();
    32.  
    33.         textFromFile = allLines[rnd1.Next(allLines.Length)];
    34.  
    35.  
    36.     }
    37. }
    38.  
    39.  
     
  6. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I changed script into this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using UnityEngine.UI;
    5.  
    6. public class TextReadFromFile : MonoBehaviour {
    7.  
    8.         Text text;
    9.     public string textFromFile;
    10.  
    11.  
    12.     void Awake()
    13.     {
    14.         text = GetComponent <Text>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         text.text = "Napis::: " + textFromFile;
    20.      }
    21.  
    22.  
    23.     private void ReadFromFile()
    24.     {
    25.  
    26.  
    27.         StreamReader reader = new StreamReader("Resources/quotes.txt");
    28.         string s = reader.ReadLine ();
    29.  
    30.         while(s != null)
    31.         {
    32.             System.Random rnd1 = new System.Random();
    33.        
    34.             s = reader.ReadLine ();
    35.             textFromFile=s;
    36.         }
    37. }
    38. }
    39.  
    But it doesn't work neither and I don't get any errors:/ There is only a "Napis::: " appearing on a screen, without textFromFile.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TextFilePlay : MonoBehaviour
    6. {
    7.     string[] lineArray;
    8.  
    9.     void Start()
    10.     {
    11.         // <your project>/Assets/Resources/TextDoc.txt
    12.         TextAsset myFile = (TextAsset)Resources.Load("TextDoc.txt");
    13.         lineArray = myFile.text.Split("\n"[0]);
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if(Input.GetKeyDown(KeyCode.Q))
    19.         {
    20.             Debug.Log(lineArray[Random.Range(0, lineArray.Length)]);
    21.         }
    22.  
    23.     }
    24. }
    25.  
    I take no credit... I've pulled this together from a couple of unity answer threads :)
     
    tarzanno likes this.
  8. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    Here's how I'd do it

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;                    // For parsing text file, StringReader
    4. using System.Collections.Generic;    // For List
    5.  
    6. public class FileIO : MonoBehaviour
    7. {
    8.     public TextAsset wordFile;                                // Text file (assigned from Editor)
    9.     private List<string> lineList = new List<string>();        // List to hold all the lines read from the text file
    10.  
    11.     void Start()
    12.     {
    13.         ReadWordList();
    14.         Debug.Log("Random line from list: " + GetRandomLine());
    15.     }
    16.  
    17.     public void ReadWordList()
    18.     {
    19.         // Check if file exists before reading
    20.         if (wordFile)
    21.         {
    22.             string line;
    23.             StringReader textStream = new StringReader(wordFile.text);
    24.            
    25.             while((line = textStream.ReadLine()) != null)
    26.             {
    27.                 // Read each line from text file and add into list
    28.                 lineList.Add(line);
    29.             }
    30.            
    31.             textStream.Close();
    32.         }
    33.     }
    34.  
    35.     public string GetRandomLine()
    36.     {
    37.         // Returns random line from list
    38.         return lineList[Random.Range(0, lineList.Count)];
    39.     }
    40. }
     
    NomadKing and tarzanno like this.
  9. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Thanks guys, pixpusher2 solution helped me a lot.
     
  10. CevdetArkun

    CevdetArkun

    Joined:
    May 30, 2016
    Posts:
    6
    Its been a while but still I did use Pixpusher2's script, assigned it to a textobject in unity display.log says it is picking a random line from my txt file but....
    1. it is not showing in the text box
    2. how can it pick a new line from the text file every 30seconds or something?
     
  11. youness12345

    youness12345

    Joined:
    Jan 6, 2019
    Posts:
    11
    welcome
    Please send me pictures showing how I can read and write in the user interface and save it in a .txt file
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Please make a new post instead of replying to an older thread, and more clearly description what you're trying to accomplish.
     
    radacadabra likes this.