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

StreamWriter and Logs

Discussion in 'Editor & General Support' started by Deleted User, Jul 5, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hi guys.
    I need to write logs to text file,so here is my code:
    Code (CSharp):
    1.  
    2. static StreamWriter writer;
    3.  
    4.     void Start()
    5.     {
    6.         writer = new StreamWriter(Application.persistentDataPath + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar + "LogFile.txt");
    7.     }
    8.  
    9.     public static void WriteLog(string input)
    10.     {
    11.         try
    12.         {
    13.             writer.Write(input + "\n");
    14.         }
    15.         catch (System.Exception ex)
    16.         {
    17.             Debug.LogError("ERROR\n" + ex.Message);
    18.         }
    19.         finally
    20.         {
    21.             writer.Close();
    22.         }
    23.     }
    24.  
    But when i try to call this function from another class via:
    Code (CSharp):
    1.  
    2. DebugLog.WriteLog("Test");
    3.  
    I see in the console:
    NullReferenceException: Object reference not set to an instance of an object
    DebugLog.WriteLog (System.String input) (at Assets/Scripts/Debug/DebugLog.cs:21)
     
  2. Deleted User

    Deleted User

    Guest

    I'm able to do this in VS 2013,but i can't do this with Unity.What's wrong?
     
  3. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    is your DebugLog script attached to any gameobject as you create the writer in start method which is not run if its not attached to any object? also if it is then you need to make sure that script run order is correct

    half static half monobehaviour classes are not good idea
     
  4. Deleted User

    Deleted User

    Guest

    Thanks,but I have already noticed this.
    Now it throws:
     
  5. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    have you created the logs folder there? it will not get autocreated by anything
     
  6. Deleted User

    Deleted User

    Guest

    Yes,before I will write something, I check if there is a folder:
    Code (CSharp):
    1.  
    2. static string path = Application.persistentDataPath + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar;
    3. if (!Directory.Exists(path))
    4. {
    5. Directory.CreateDirectory(path);
    6. }
    7.  
    Folder is created, but it still throws an exception:
     
  7. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    did you actually check with explorer it got created and exists (and code was run)? error is still suggesting its not there

    hit Windows+R and paste "C:\Users\Home\AppData\LocalLow\DefaultCompany\New Unity Project\Logs\" with quotes
     
  8. Deleted User

    Deleted User

    Guest

    Thanks,I managed to solve it,the problem was actually in other thing :D
     
    Last edited by a moderator: Jul 6, 2014