Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
7
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
Add comment
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
34
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
Add comment · Show 3 · 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 0
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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Loading Json from Resources via Editor Script 1 Answer

how to parse a json feed 1 Answer

Null Exception while serialize json string into txt file 0 Answers

When I Build my game it cannot load a JSON file. 0 Answers

calling Encoding.UTF8.GetBytes(JSON) in a dll causes exception, while in src format everything works great. 2 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