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 fireDude67 · Aug 02, 2010 at 07:08 PM · editor-scriptingassettextasset

How to assign variables like Texture2D, TextAsset, Transform, etc. using code?

I'm trying to make a mission system for my game, and I need to assign multiple Text files into an array of TextAssets. The easiest way to do this would be to loop through the directory which contains the files, and then assign them to the array. How would I do that?

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

Answer by qJake · Aug 02, 2010 at 07:17 PM

You don't. Everything is done via the Inspector using references to assets. This way, Unity can optimize your game as much as possible (since reading and writing directories like that at runtime can be slow).

If you make a public TextAsset[] variable, you can fill that in using the Inspector.

But if you really want to read the assets from a directory, you can put everything into folders inside one of the special Assets folder, /Resources/. Everything in there will be included at runtime, so you can grab stuff out of there if you want to, like this:

TextAsset myAsset = Resources.Load("myTextFile.txt") as TextAsset;

"myTextFile.txt" needs to be in the Resources/ folder at that point. You can also use subfolders.

Comment
Add comment · Show 4 · 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 fireDude67 · Aug 02, 2010 at 07:20 PM 0
Share

Thanks, that was what I was looking for!

avatar image Julian-Glenn · Aug 10, 2010 at 01:06 AM 0
Share

Great answer. Solved a problem for me today.

avatar image qJake · Nov 25, 2010 at 11:31 PM 0
Share

No it shouldn't. Either way is fine, I just prefer using as.

avatar image pgomes · Apr 18, 2012 at 04:24 PM 0
Share

You shouldn't include the "txt" file extension in the Resources.Load call. See: http://answers.unity3d.com/questions/57968/resourcesload-driving-me-a-bit-crazy.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

No one has followed this question yet.

Related Questions

Get path of editor script? 2 Answers

Resources.FindObjectsOfTypeAll() doesn't return all Textures 2 Answers

Programmatically Exporting an Asset Package 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Build from script 3 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