Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

reasons for Resource.Load failing?

Discussion in 'Scripting' started by fstim82, Nov 15, 2011.

  1. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    The only reason in the documentation is not finding the file. I've checked the spelling of the prefab file I'm trying to load, and it is in the Resources folder (which is also spelled correctly). The load function just seems to be returning NULL anyway, and I don't have any indication why...
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Are you adding the Extension into the name you are requesting(don't do that)?
    Are you putting the whole path into the Load function (don't do that either)?
    Are you using the Function overload where you specify Type, and choosing the wrong Type?
    What kind of Resource is it?
    Can you elaborate with code, and a description of file system structure/file name/type?
     
  3. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    No extension, no path, the file is at Assets/Resources/TestHeartShapePrefab.prefab and I'm trying to load "TestHeartShapePrefab", not using the Type version of load(), and again it's a prefab. I'm using windows 7. The code is nothing fancy, just a simple test:

    Object test = Resources.Load("TestHeartShapePrefab");

    Afterward, I've tried doing a long loop to stall for time, since I wasn't sure if it was an asynchronous load. Still, the test object is null.... unless the monodevelop debugger is lying?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    It's not; if it was, the docs would say so, and it would have to work in a similar way as, say, WWW.

    --Eric
     
  5. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    Yeah, I was coming to that conclusion after trying to immediately Instantiate() the Object like the examples do, and getting a "The thing you want to instantiate is null." error in the console. I've noticed the Resources folder is marked as read only, which I assume Unity is imposing and is fine. Tried removing it and it came back automatically. I only mention it because I'm out of ideas here...
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Unity shouldn't be imposing the Resources folder as read-only. Are you using some sort of source control? (Perforce, svn)
    I'm out of ideas, it seems like a really simple use for Resources.Load and I've not seen that sort of issue.
     
  7. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    Yes, Perforce is involved. I'd not added the prefab to the depot yet, though the Resources folder is, and after marking the prefab for add, it still seems to have the problem. I wouldn't expect a read only flag to keep the file from being loaded anyway... I'll keep messing around with it.
     
  8. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Checkout Resources as a whole just in case. Resources itself gets converted to an LMZA compressed blob file, and so if it can't write new files maybe that is causing some sort of issue.

    Also try a copy of the build entirely out of Perforce to make sure it's not some other readonly folder where it's trying to write the Resources blob for in editor use (really unsure if it uses the LMZA blob in editor, but it does in standalones, and your issue is odd enough that could be it).
     
  9. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    I've made a copy of the project folder in attempt to remove Perforce's influence. Oddly enough, removing the read only flag from the root folder, it seems to automatically set the flag again on all folders, but not files, immediately. Not sure yet why this is...
     
  10. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Library folder can do that I believe, but Assets shouldn't
     
  11. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    Googling the issue, its seems that the folders aren't really marked read only:

    When you look at a folder's properties, the read-only box will always show the half-way gray state. This means "don't make any changes to read-only flag on the files in this folder". You can check (or clear) the box to mean "make all the files in this folder read-only (or read/write)."

    So I guess that isn't actually an issue. Now I'm back to square one. I appreciate your help so far BTW.
     
  12. fstim82

    fstim82

    Joined:
    May 4, 2010
    Posts:
    28
    SOLUTION:

    I believe the prefab I was attempting to load was created during a session of Unity in which a bad script crashed the program, leaving me with a prefab that wasn't properly saved/connected in some way. I realized this when trying to drag the prefab onto the scene in the editor and it wouldn't work. Despite the prefab appearing correct (blue square), it simply didn't actually exist, as if it should have been a grey square'd prefab entry. It would be great if there were some kind of warning given for this "null prefab" scenario, or at least an addition to the Resources.Load documentation suggesting that the prefab might be bad, rather than just "not found".
     
    Last edited: Nov 15, 2011
  13. zzxiang

    zzxiang

    Joined:
    Aug 29, 2015
    Posts:
    6
    ANOTHER SOLUTION:

    I encountered the same issue, and it can be solved by simply reimporting those prefabs in the editor.