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

Reading text from a text file on the disk

Discussion in 'Scripting' started by yuki0okami, Jul 21, 2022.

  1. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    Good morning, I'm a beginner. I have a working code that assigns the text object (answer_text) the content that we enter in InputField (answer_type) and saves in the registry and loads this content when restarting the program thanks to PlayerPrefs:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6. using System.Linq;
    7. using TMPro;
    8. using UnityEngine.Events;
    9. using UnityEngine.EventSystems;
    10.  
    11. public class saveinput3 : MonoBehaviour
    12. {
    13.  
    14.  
    15.      [SerializeField] TextMeshProUGUI answer_text;
    16.      [SerializeField] TextMeshProUGUI answer_type;
    17.    
    18.  
    19.  
    20.  
    21.    
    22.     void Start()
    23.     {
    24.        answer_text.text = PlayerPrefs.GetString("answer_user");
    25.     }
    26.  
    27.     public void SaveIt()
    28.     {
    29.         answer_text.text = answer_type.text;
    30.         PlayerPrefs.SetString("answer_user", answer_text.text);
    31.         PlayerPrefs.Save();
    32.     }
    33.  
    34. }
    35.  
    The thing is, I don't want to write it to the registry but to a text file, and I can't make it work.
    I did something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6. using System.Linq;
    7. using TMPro;
    8. using UnityEngine.Events;
    9. using UnityEngine.EventSystems;
    10.  
    11. public class saveinput4 : MonoBehaviour
    12. {
    13.  
    14.    
    15.     TextMeshProUGUI readFromFilePath;
    16.  
    17.      [SerializeField] TextMeshProUGUI answer_text;
    18.    
    19.    
    20.  
    21.  
    22.  
    23.  
    24.     void Start()
    25.     {
    26.        string readFromFilePath = Application.streamingAssetsPath + "/Sound/" + "answers" + ".txt";
    27.     }
    28.  
    29.     public void SaveIt2()
    30.     {
    31.         answer_text.text = readFromFilePath.text;
    32.  
    33.     }
    34.  
    35. }
    36.  
    The project compiles without complications. However, that when I call the "SaveIt2" assigned to the button, then I get:

    NullReferenceException: Object reference not set to an instance of an object
    saveinput4.SaveIt2 () (at Assets/Scripts/saveinput4.cs:31)
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
  3. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    haha "Step by step, break it down, find the problem and fix it!" Thanks for this advice, but it's completely useless
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
    It's only useless if you don't follow those steps and don't put any work in. I would suggest that you start by trying to understanding what a Null Reference is i.e. what C# value type and reference type are. Because the error tells you the exact line it's on (Line 31) it's simple to debug it i.e. find what is NULL then you have to find out why it's NULL by looking where you think you're assigning it something.

    You should start by finding out if it's the "answer_text" object or the "readFromFilePath" object that is NULL.
     
    Last edited: Jul 21, 2022
  5. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    Why are you programmers always like this? Never in my life asking animators or graphic designers did anyone offend me like that - "look at it and fix it yourself, it's simple" or "go and learn animation / graphics", but with programmers it is like that every time. Such responses alienated me from programming 10 years ago, and it's the same now.

    I do not understand this, therefore I am asking for help, if I understood it, I would not ask for help, if you do not want to help, do not fill up the comment counter at my expense.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
    I am not trying to argue with you and I don't see how you are being offended. You want help but it seems you want to be given the answer. You cannot be given the answer by posting code, it doesn't provide the answer. You can only be given advice on how to find it yourself so the debugging link I gave you, suggesting you try to understand what the error is by looking up what C# reference/value types are, telling you the line and that it can only be one of those two objects I explicitly named above.

    I am not sure what else you want in terms of help but obviously nothing I am providing you so I'll leave it there.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    This clearly shows that you did not read the post. Nobody here can fix your error. 100% of the information on the error is in front of you on your screen, NOT on our screen.

    Don't you think that if we COULD fix it, I would have instead just posted "Change this line of code" instead of the actual three-step process??

    The longer you delay the three-step process, the longer it takes for you to get back on track.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that

    The three step process is used TO understand it. Every minute you delay is a minute of your project not being fixed.

    Your move.
     
  8. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    You are like from another planet. This forum is full of ready answers. If my code is that simple, why can't I get this ready answer as well?
    I don't understand these three steps. You don't understand what it means not to understand?
     
  9. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    It seems like the error is telling you something is null on line 31 of saveinput4.

    It can only be answer_text or readFromFilePath. (or maybe both).

    Line 17 is serialized and so is probably showing on your script in the inspector, have you drag/dropped a TextMeshProUGUI component in to that reference in the inspector? If not, then answer_text is null. You declared in on 17, but haven't pointed it at anything, hence it points at nothing, aka the reference is null.

    readFromFilePath.text on line 31 will have a problem too. On line 26, you're declaring a new string there. I think you meant for it to be stored in line 15 so you can use it on line 31. If so, remove the word 'string' from line 26, make line 15 a string instead of a TextMeshProUGUI and that's fixed too. Give line 15 a pubic access modifier and you'll be able to see it in the inspector when you press play.

    Before moving on, be sure to make your class names PascaleCase and your field names at the top camelCase so that it's consistent with the rest of Unity's naming conventions. It's easy that way to know what you're looking at if you can tell by the casing.

    Next thing to work on might be splitting up line 26 in to multiple strings to make it easier to chase bugs. Maybe one for application path, one for path, one for file name, then a final one for all of them joint together.

    Then you'll need to look in to how to that path you have stored to read from a text file to get the text in to your game. Not as I use JSONnet deserializer to do this for me.

    I'm not far from boob myself, sometimes 'simple' obvious things to experienced programmers can sound completely alien! It probably looks a lot like laziness, but it's often just big knowledge gap I think.

    It'd be worth looking for a C# fundamentals course on YouTube and working through that. It'll make things a lot easier imo. It won't take long and It's harder to learn this in a game engine imo.
     
    Last edited: Jul 21, 2022
    Brathnann and Kurt-Dekker like this.
  10. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    "remove the word 'string' from line 26"
    gives me

    Assets\Scripts\saveinput4.cs(26,28): error CS0029: Cannot implicitly convert type 'string' to 'TMPro.TextMeshProUGUI'

    "make line 15 a string instead of a TextMeshProUGUI"
    gives me

    Assets\Scripts\saveinput4.cs(31,45): error CS1061: 'string' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
     
  11. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    If you want to read/write to a text file, you're probably going to want to learn more about the .NET System.IO Namespace. Honestly, I would recommend watching a tutorial on the topic, as I'm pretty sure they can show it better than anyone on here. The code you have isn't really even salvageable (no offense).

    Here's one I found pretty quick, hope it matches what you're looking for.

     
    Brathnann and Kurt-Dekker like this.
  12. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    This video is the first thing I came across when looking for a solution. His script is written under the old version of unity and DOES NOT WORK!
     
  13. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    It does work, System.IO is written by Microsoft, not Unity. Nothing else in their code is out of date either.
     
    passerbycmc and Brathnann like this.
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Nonsense. You have a nullref. Fix it. You're arguing that a shoe does not work because the shoelace is untied. Tie the shoelace and you'll be fine. The three-step process tells you how to find which shoelace is untied and how to tie it.

    I find it luridly fascinating fascinating to see how long people resist the three step solution to fix a NullRef. I think the record so far was about six days of continuous back and forth posting. This is about the 36-hr mark for this one so we have a ways to go.
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    The reason for the steps is because a null ref error is one of the most common errors you will encounter. It's so common that even after nearly 25 years of programming and using various languages, I will still get them myself. And honestly, the same three steps apply to me as well, though I might be able to go through it a bit faster, I still have to solve the what and why before I can fix it.

    Also, I would much prefer someone give me steps to solve something and let me attempt to figure it out myself, especially if it would help me in the long run! Imagine wanting to edit a keyframe on one animation and being told exactly how to do it, just to have to ask again on another keyframe!

    Why can't we just give you the answer? Because it's not always as simple as "just do this". @mopthrow gave you a ton of information and you followed it without really understanding what those changes would do, based on your response, which just generated more errors. But their response contained so much useful information and was trying to decipher what you were trying to do with your code.

    @MelvMay also gave you information on how to solve the null error. Trying to explain the three steps better as they apply to your code, with your response being that they were insulting you, which makes me wonder if you read the entire response.

    @RadRedPanda gave you a very useful video link that gave you an answer for how to write to a file and I wonder, did you try it? It feels like you didn't, again because of your response.

    I was going to add some information here, but after looking at all the help you have been given, I'm not sure what more I can add that would be useful.
     
    passerbycmc, mopthrow and Kurt-Dekker like this.
  16. yuki0okami

    yuki0okami

    Joined:
    Apr 23, 2021
    Posts:
    7
    Instead of writing advice that gives me nothing at the same time, you would write 30 times the finished code I am asking for already.

    Current Unity uses TMPro. This gentleman's script in the video does not refer to TMPro, so it does not work. I tried to change it to be under TMPro but I can't.
    Because I suck in programming.
    That's why I use visual scripting. But the one thing I am asking cannot be done in visual scripting.

    Therefore, let's agree like this; the topic will be opened, because maybe one day someone will take pity and write these few lines of working code for people like me.
    And you will stop posting here, for the sake of my and your mental health. Okay?
     
  17. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    You see your problem? people here (or anywhere) don't just give free things, writing code is the main job for most people here, so what you're asking for is literally unpaid labor. You won't ask a plumber to repair your faucet for free right? you may ask him (very nicely) to explain to you how to do it yourself, and if he's a good guy (like the folks here) he may do it if he thinks you have basic plumbery skills and you can do it yourself...

    Think about it...

    You won't go far in this industry (or any other for that matter) with this attitude.
     
    passerbycmc likes this.
  18. Predulus

    Predulus

    Joined:
    Feb 5, 2018
    Posts:
    90
    @yuki0okami I understand why you were offended - sometimes the 'tone' used by tech people can be slightly (or very) abrasive in answering questions.

    I think the differences you are experiencing with some of the people above stem from what you are expecting/hoping for, versus what the aim of these forums is. Generally when I post questions here, I am not asking someone to give me the working code, but asking for clues on how to fix errors that I have.

    You mention that you are a beginner - absolutely no shame in that - we all have to start there. But perhaps you are trying to run before you can walk, a little? In order to write code you will need to understand some key concepts.

    Some of the ones you need include:

    - What is a reference?
    - What does it mean for a reference to be 'null'?
    - If the compiler tells me there is a null reference on line XYZ, how can I find which variable on that line contains the null reference?

    I could write the code for you, but that would only help you in the immediate case. The more useful thing for me to do, is to point those few things out.

    I wish you the best, and don't be put off! Good luck!


    __________________

    P.s. a couple of 'freebie' hints:

    line 31 is: answer_text.text = readFromFilePath.text;

    This line only contains two object references (the things containing the periods)

    They are: answer_text, and readFromFilePath

    So the question is, which one is null? And why wasn't that object instantiated.
     
    Nad_B and passerbycmc like this.