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 Branislav · Oct 26, 2011 at 03:42 PM · iosresources.load

Resources.Load fails when called the second time

I have a code that loads a resource at the runtime using Resources.Load, and that resource is a prefab (in the Resources folder). When I call this code at the start of my level, the resource is loaded and everything works fine.

When my level is complete I Destroy the object (that is loaded at the start) and call UnloadUnusedAssets.

But when I start the level again, and call the same code (from the beginning), Resources.Load returns null (even though I am loading the same resource that worked fine the first time).

Also this does not happen in the editor, just on the iPad.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by maierp · Nov 26, 2011 at 12:43 PM

I think I have the answer. Dis you had a code like this:

 GameObject chair = (GameObject)Resources.Load("chair", typeof(GameObject));
 car.transform.parent = gameObject.transform;
 car.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
 Instantiate(car);

In this case, you set this new object as the child of an object in your Scenegraph/Hirachy before you do the instanciation. Here the complete object is then discarded, when you unload your scene. I think some of the resource is also discarded in this case. As Unity does not recreate the resource after it has been loaded once, the resource-object gets a bit messed up. When you try to load it again, it does not recognize it anymore as a GameObject, thus it is not loaded and the result is null.

Instead of the code above, you should instanciate your loaded GameObject before you set the parent property on the cloned Object:

 GameObject chair = (GameObject)Resources.Load("chair", typeof(GameObject));
 GameObject c = (GameObject)Instantiate(chair);
 c.transform.parent = gameObject.transform;
 c.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);

I hope this helps (even so long time later)

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 Daedin · Jan 15, 2013 at 05:02 PM 0
Share

This actually helped me a lot :) I can't vote for your answer though (don't have the permission for it error). Thanks!

avatar image
0

Answer by maierp · Nov 25, 2011 at 03:07 PM

I also have this behavior. I've a GameObject in the "Resources" folder which I can load one time:

 car = (GameObject)Resources.Load("chair");

Then the level quits and I restart the scene. This line is executed again, but this time the Resources.Load("chair") function does not return a GameObject, it returns a Mesh-Object.

How can I get again the GameObject?

Thanks and Greets Patrick

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 mkelly4ca · Feb 02, 2013 at 02:41 AM

I found that if you Destroy the asset, Resources.Load will return null for that asset from then on. Instead you need to call Resources.UnloadAsset.

Instead of:

 TextAsset bindata = Resources.Load("SomeAsset") as TextAsset;
 DoSomethingWith(bindata);
 Destroy(bindata);
 // Resources.Load("SomeAsset") will return null from now on

Do this:

 TextAsset bindata = Resources.Load("SomeAsset") as TextAsset;
 DoSomethingWith(bindata);
 Resources.UnloadAsset(bindata);
 // Resources.Load("SomeAsset") will succeed again next time
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

Resources.Load not working in flash build. I Can't find answer anywhere. 0 Answers

Resources.load vs LoadLevelAdditive which is faster 1 Answer

Unity 5 Resources.LoadAsync on Android slower than iOS? 0 Answers

Can I load textures at runtime with a smaller memory footprint? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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