• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by William_Goodspeed · Nov 03, 2015 at 08:55 PM · c#jsonresources.loadstreamreader

Loading JSON as a Text Asset using Resources.Load

Hey folks,

So I've got a bunch of .json files in my Resources folder, they are contained within several sub folders. And I want to Load them, before passing them through StreamReader and returning the final text.

However, when I try to Load the file I'm encountering a Null Ref of - "NullReferenceException: Object reference not set to an instance of an object"

Now I know this suggests the file isn't there but it is and I've got some Debug showing that the path is correct. Below is my code:

         public static string LoadResourceTextfile(string path)
         {
             string text;
             string filePath;
 
             filePath = @"SetupData/" + path;
            // filePath = filePath.Replace(".json", "");
 
             Debug.Log("filePath Is: " + filePath);
 
             var targetFile = Resources.Load<TextAsset>(filePath);
 
 
             using (var streamReader = new StreamReader(targetFile.text, Encoding.UTF8))
             {
                 text = streamReader.ReadToEnd();
             }
 
             return text;
         }


The "filePath" mentioned above which is passed in actually has the .json suffix, which is why I have a commented out replace. But that didn't fix the issue in any case.

Suggestions would be very welcome :).

Comment
dog_funtom
circuitjump
CelitoOokaIsland
mkusan
Bunny83
dappledore
Estudio_Haus
starikcetin
jerobarraco
alemfi538

People who like this

10 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by William_Goodspeed · Nov 04, 2015 at 10:30 AM

In the end I found that simple removing the StreamReader was the easiest way to solve my issues.

         public static string LoadResourceTextfile(string path)
         {
 
             string filePath = "SetupData/" + path.Replace(".json", "");
 
             TextAsset targetFile = Resources.Load<TextAsset>(filePath);
 
             return targetFile.text;
         }

Comment
pixxelbob
pango
shaneparsons
owntheweb
funamiyui
circuitjump
CelitoOokaIsland
idchlife
diogoico
DarkGate
mkusan
Piyush_Pandey
Bunny83
Dinochrome
gavinmannion
And 24 more...

People who like this

39 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piyush_Pandey · Mar 22, 2018 at 11:41 PM 0
Share

You saved my life with this answer dude. Godspeed!

avatar image Tsuxi · Jun 19, 2019 at 01:15 AM 0
Share

Thanks! That's work for write and read json files :) you saved me too

avatar image AndyMartin458 · Oct 04, 2019 at 10:01 PM 2
Share

Yeah, I had to remove the .json as well. None of the paths have a suffix in the documentation, so I guess that is a clue. https://docs.unity3d.com/ScriptReference/Resources.Load.html

avatar image atnicoconut · Aug 02, 2022 at 03:13 PM 0
Share

Thanks! For me it worked without removing the .json(Unity 2020.3).

 string filePath = "Recipes/";
 
         TextAsset[] jsons = Resources.LoadAll<TextAsset>(filePath);
 
         foreach(TextAsset json in jsons)
         {
             MyClass classFromJson = JsonUtility.FromJson<MyClass>(json.text);
 
             myStuffList.Add(classFromJson);
         }

Dunno if that helps anyone :)

avatar image alemfi538 · Nov 28, 2022 at 12:33 AM 0
Share

Weird, why is it specifically json that requires removing the file extension? As far as I know, loading other text files is fine with the extension? It looks like this was a separate problem from your original query.

Thanks from 2022 btw.

Using Unity 2021.1.3.14f1.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JSONUtility FromJson function causing memory access out of bounds error 0 Answers

How can I find the all the Vertices of several Game Objects that have a same Tag? 0 Answers

Refresh Chat room messages 1 Answer

Storing Level Data in Json Text in Streaming Asstes Folder 1 Answer

Write a list of custom classes to JSON in a human readable/editable format 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges