Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
2
Question by Wontoon · Jul 29, 2015 at 11:44 AM · nulltextasset

TextAsset always returns null despite the file existing?

I'm trying to have Unity read from text files, and oddly enough it returns a null TextAsset for me every time. I have tried several things to get it to work, and searching here on Answers and Google yielded nothing, and checked the documentation. I also checked here and while there were some questions related to this, the answers are oddly not working.

Now I hope I read it right, but TextAsset can't be used for writing text files; only reading.

 dbfile = "/GameData/items.txt"
 
 TextAsset t = Resources.Load (dbfile) as TextAsset;
 
 if (t == null)
         Debug.Log ("No Textasset! D:");



Unfortunately, I always got null. It seems to be finding the file (otherwise I'd get a FileNotFound exception) I have tried several things to get this working (based on the answers I found):

-Removed the ".txt" extension.

-Renamed GameData to Resources according to one post.

-Removed the first "/" character.

-Throw items.text to the "Assets" folder.

-Tried another method of casting TextAsset (via TextAsset t = (TextAsset)Resources.Load("/GameData/items.txt",typeof(TextAsset)) )

All, if they didn't give me a file not found exception, only returned null. (Reading the file using the File.ReadAllLines and etc works just fine, so the file is there). Am I doing something else wrong with TextAsset/am wrong to assume the TextAsset class can be used to get data from a text file bundled as an asset?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jan 24, 2016 at 08:06 AM

The only valid path would be

 dbfile = "GameData/items";

However make sure you placed your items file here:

 "/Assets/Resources/GameData/items.txt"

"Resources.Load" can only load resources which are in a "resources" folder. The path is always relative to the resources folder.

edit
The usual prefered way would be to just declare a public variable like that:

 public TextAsset itemsDB;

and assign your text asset file in the editor to the variable. This has several advantages:

  • The file doesn't need to be in a resources folder. It can be anywhere inside the assets folder.

  • Since you assigned the reference in the editor, Unity doesn't have to search for the asset in all resources folders. The reference is serialized.

  • Unity automatically detects all assets which are referenced by some serialized variable and only includes those assets into a build. Things inside Resources folder are always included, no matter if you use it or not since you can load them dynamically via code.

Comment
Add comment · 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
0

Answer by subject119 · Jan 24, 2016 at 04:02 PM

I am encountering the same issue. Any update on this?

Comment
Add comment · Show 1 · 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 subject119 · Jan 24, 2016 at 06:31 AM 0
Share

I have solved my issue. $$anonymous$$y problem is that I have ".bin" as my binary file extension, while it needs to be ".bytes" ins$$anonymous$$d when using TextAsset. After changing the extension to ".bytes", Load() works as expected.

avatar image
0

Answer by CatsEleven · Mar 11, 2017 at 02:26 PM

I had a very similar problem and since I can't find much help on fixing it, I'll post it my solution here.

I was creating bytes files then trying to read them as text assets afterwards and they were coming through as null even though the file existed. The problem turns out to be that it was missing a meta file that contained text import information which wasn't being built for it during runtime.

Calling AssetDatabase.Refresh forced Unity to generate the meta files and fixed the problem however I havn't tested it from a build so its not a perfect solution.

I hope this sheds some light on anyone stuck with the same kind of problem. Credit to this answer for helping me.

Comment
Add comment · 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
0

Answer by astracat111 · Oct 30, 2018 at 07:48 PM

For me this was solved...

Basically I was reading a file called MG_Data.data that contained my entire games xml database. The problem is that if something is named '.data' it's not gonna load. It MUST have the '.txt' extension to be loaded using Resources.Load(pathNameGoesHere);

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Load JSON Resource as TextAsset 3 Answers

Problems with Loading a XML File for a TileMap using XMLTextReader 1 Answer

textasset and how to use them 0 Answers

Access components in children of a prefab? 5 Answers

Null values in Unity 1 Answer


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