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. Dismiss Notice

Question Pulling Text from fifth line of .txt file via File.ReadLines

Discussion in 'Scripting' started by CarelessCrasher, Jul 6, 2023.

  1. CarelessCrasher

    CarelessCrasher

    Joined:
    Jan 27, 2022
    Posts:
    1
    Okay, so I'm running into some issues here. So let me explain what I'm trying to do:

    I'm trying to make a development log record system, that pulls information from certain lines of a .txt file and replaces the text on a TextMeshPro element. I've figured out how to change the text, and I'm now trying to figure out how to pull line 5 from a text file.
    upload_2023-7-6_15-14-4.png
    Each devlog has a number, and the search system searches for the number of the file, and pulls line five from it to change the devlog textbox. The plan is to change the search number by 1 depending on the key pressed, and then search for a devlog matching the new number, and then change the current TextMeshPro elements onscreen. Note: the method that changes the number hasn't been created yet, but the variables for it have.

    Now, I'm almost positive that I have the filepath of the devlogs correct. And there's tons of different websites with convoluted statements on how to use the File.ReadLines script, so my guess is that I'm getting the formatting wrong, or something else that's either really simple, or really difficult.

    It keeps giving me a bunch of error messages, and no matter how many changes I try, it just seems to add more. Honestly, at this point, I'm stuck, so I'm asking for help on here.


    Here is the code, for reference.

    Code (CSharp):
    1. using System.Collections.Generic;
    2.  
    3. public class DevlogChanger : MonoBehaviour
    4. {
    5.     public TMP_Text CanvasText;
    6.     string DVLGFilePath;
    7.     public int Entry;
    8.     public int EntrySearch;
    9.     string[] DVLGLinesArray;
    10.  
    11.     // Start is called before the first frame update
    12.  
    13.     void Start()
    14.     {
    15.         Entry = 1;
    16.         EntrySearch = 1;
    17.         DVLGFilePath = Application.dataPath + "/" + "Devlogs" + "/" + "1" + ".txt";
    18.  
    19.     }
    20.  
    21.     public void SearchForEntry(string DVLGFilePath, int EntrySearch)
    22.     {
    23.         //search for file matching new int
    24.         DVLGFilePath = Application.dataPath + "/" + "Devlogs" + "/" + EntrySearch + ".txt";
    25.  
    26.     }
    27.  
    28.     public void UpdateDevlogText(string DVLGFilePath, int EntrySearch)
    29.     {
    30.         IEnumerable<EntrySearch>, File.ReadLines(DVLGFilePath, path);
    31.      
    32.  
    33.         foreach (string line in DVLGLinesArray)
    34.         {
    35.             print(line);
    36.         }
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.  
    43.         if (Input.GetKeyDown(KeyCode.Space))
    44.         {
    45.             CanvasText.text = "output from line search will be put here";
    46.         }
    47.     }
    48.  
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    you have a wrong comma on line 30, fix it first.

    If you don't read the error messages how do you expect to solve the bugs?
    If you don't post the error messages here, how do you expect us to guess the bugs?
     
  3. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    Also here is some additional help:

    Your SearchForEntry method doesn't need DVLGFilePath to be passed as a parameter, the class it already has a field called DVLGFilePath. You can't modify the DVLGFilePath parameter anyways, because strings are passed as value.

    For UpdateDevlogText, just iterate the lines of a file like every other example does, you don't need to use a temporary variable to store File.Readlines:

    https://www.geeksforgeeks.org/file-readlinesstring-method-in-c-sharp-with-examples/
    Code (CSharp):
    1. foreach(string line in File.ReadLines(@"file.txt"))
    2.         {
    3.             // Filtering the file contents and printing
    4.             if (line.Contains("GFG")) {
    5.                 Console.WriteLine(line);
    6.             }
    7.         }
    You are literally overriding the values typed from the editor, if you want to set default values to 1, just do it when declaring the variable