Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
1
Question by Hypnotoad0 · Aug 06, 2015 at 12:47 PM · scriptableobjectresources.loadall

Loading scriptable objects from resources.

I need a dictionary for my items so I can reference them using their IDs. My items are scriptable objects. Theyre all in my "Resources/Items" folder.

Im trying to get them using Resources.LoadAll but it doesnt work, im getting a casting error:

 InvalidCastException: Cannot cast from source type to destination type.

This is how my Resources.LoadAll looks:

 Item[] foundItems = (Item[])Resources.LoadAll("Items/", typeof(Item)) as Item[];

As you can see I've been trying to fix it by putting Item[] everywhere but it doesnt help.

Comment
Add comment · Show 3
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 Mikilo · Aug 06, 2015 at 12:55 PM 2
Share

You might try Resources.FindObjectsOfTypeAll()

avatar image Hypnotoad0 · Aug 06, 2015 at 01:08 PM 0
Share

That worked. But Im really wondering why Resources.LoadAll doesnt work, it works fine for prefabs.

avatar image Mikilo · Aug 06, 2015 at 01:14 PM 1
Share

Glad it works for you. =D

I dont know too. But I had this issue, hence the solution.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Hypnotoad0 · Aug 06, 2015 at 01:06 PM

 Item[] foundItems = (Item[]) Resources.FindObjectsOfTypeAll(typeof(Item));

That worked for 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
1

Answer by Dave-Carlile · Aug 06, 2015 at 12:52 PM

Have you tried this?

 Item[] foundItems = (Item[])Resources.LoadAll("Items");


That most closely matches the example in the documentation.

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 Hypnotoad0 · Aug 06, 2015 at 12:56 PM 0
Share

Yes, this was the first one Ive tried.

avatar image
0

Answer by tonytopper · May 26 at 07:47 PM

Here's a potential more modern answer to this problem:

You may be better off caching them into an array you create in the editor on a Component script. This probably wouldn't be as convenient when working with a very large amount of items that change frequently.

So if manually creating an array isn't copacetic for your use case, you could take a look at Addressables. https://docs.unity3d.com/Packages/com.unity.addressables@1.20/manual/index.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Resource.LoadAll Not Working On Child Assets In Stand Alone Build 0 Answers

Loading ScriptableObject when I open Unity 1 Answer

ScriptableObject losing data in AssetBundle 2 Answers

Set types of scripts i can place in ScriptableObject[] 1 Answer

Should I worry about having many 1000s of small files in my project? 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