Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems with loading .txt file in Textasset

Discussion in 'Scripting' started by Falin, Feb 15, 2012.

  1. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    When I use Resources.Load it will say: NullReferenceException: Object reference not set to an instance of an object.
    Code (csharp):
    1.         if(GUI.Button(new Rect(Screen.width/2,350,50,30),"Load")){
    2.             ReadText = (TextAsset)Resources.Load("/Journal" +Page +".txt", typeof(TextAsset));
    3.             GUI.Box( new Rect( 0, 0, Screen.width, Screen.height ), ReadText.text );
    4.             //Debug.Log("loaded");
    5.             print(ReadText.text);
    6.            
    7.         }
    But when I drop the Journal1.txt in the inspector to the variable(text asset) it will load.
    And how do I display it in like a GUI.Box/Window/Label etc. Because when I do it like code above it will only display in the console because of the print command but not on the actual game screen.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Make sure Page is actually set to 1. Remove the leading slash from the string path. Remove the file extension from the string path.
     
  3. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    I tried that but still get the NullReferenceException: Object reference not set to an instance of an object.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Remove the second argument in Resources.Load and make sure the txt file is actually in a folder called Resources in your project.
     
  5. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    that helped thanks,
    But also does someone knows how to display it in GUI statement?
     
  6. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    *bump*
    Is it possible to display a textasset in a GUI statement?
    Or do I need to use something like streamreader?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    TextAsset.text is just a string so any GUI element that displays a string will work fine.