Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by gilgada · Mar 15, 2013 at 03:41 PM · assetsfiletextassetdatapath

Error: get_dataPath can only be called from the main thread

I'm getting this error:

get_dataPath can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

All I'm trying to do is load a text file from the resources folder of the project. Originally I was just setting the filepath with a string and loading from that but it would not work when publishing to the web.

Code is as follows:

 TextAsset infoFile;
 public SearchField searchfield;
 
 public void Update()
 {
     if (searchfield.Confirm == true)
     {
         
         infoFile = (TextAsset)Resources.Load(searchfield.ToString(), typeof(TextAsset));    
         string[] allcontents = File.ReadAllLines(infoFile.text);
         contents = ConvertStringArrayToStringJoin(allcontents);        
     }
 }

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
1
Best Answer

Answer by PAHeartBeat · Mar 15, 2013 at 05:36 PM

 TextAsset infoFile;
     public SearchField searchfield;
     public void Update(){
         if (searchfield.Confirm == true) {
             infoFile = (TextAsset)Resources.Load(searchfield.ToString(), typeof(TextAsset));    
             string contents = infoFile.text;
             string[] allLines = contents.Split('\n');
            }
     }

Code gives above can read your text file from Resources Folder. if you don't want array of all lines in seprate field just comment line in which I use Split function.

Comment
Add comment · Show 8 · 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 gilgada · Mar 19, 2013 at 12:24 PM 0
Share

Doesnt seem to work. I tried passing 'contents' to the GUI and it doesnt show the text and reports a null reference exception

avatar image PAHeartBeat · Mar 19, 2013 at 01:10 PM 1
Share

Hi Can you provide your exact code which you writes in GUI function..

and I want to clear a thing, when you use Resources.Load() function it's not counting ext of the file. like file is Data.txt in you need to use just Data in your function.

avatar image PAHeartBeat · Mar 19, 2013 at 01:14 PM 1
Share

null reference means code line

 infoFile = (TextAsset)Resources.Load(searchfield.ToString(), typeof(TextAsset)); 

can't find any files with name which return by searchfield.ToString() in resources folder please check the file name which you want to read from resources folder

avatar image PAHeartBeat · Mar 20, 2013 at 04:13 AM 1
Share

Hi Please change the code as i gave bellow

 string allcontents
 public void Update() {
     if (searchfield.Confirm == true){
         string path = searchfield.Search";
         infoFile = (TextAsset)Resources.Load(path.ToString(), typeof(TextAsset));    
         allcontents = infoFile.text;
         contents = allcontents.Split('\n');
     }
 }

and GUI code:

 GUI.Label(new Rect(posX,posY,width,screenheight), allcontents, "label");

or

 GUI.Label(new Rect(posX,posY,width,screenheight), string.Join('\n', contents), "label");
avatar image PAHeartBeat · Mar 20, 2013 at 11:26 AM 1
Share

You are welcome bro.. and it's my pleasure to help you. Please $$anonymous$$ark question is Accepted so other new-bee developer also use it....

thanks a lot for your valuable time...

Show more comments

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

11 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 avatar image

Related Questions

Path for Assets/Textures folder File in Android 2 Answers

how to change Application.persistentDataPath? 1 Answer

Using OpenFilePanel at runtime to open a file as a TextAsset to read data is giving NullReferenceException 1 Answer

How do I find a folder I can write data to on Android? 3 Answers

Create a Textasset from a string 4 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