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
4
Question by Chaosgod_Esper · Nov 16, 2012 at 12:39 AM · texturenullreferenceexceptionloadresourcesresources.load

Resources.Load(path+name) as texture == null reference

Hi there

i´ve a small problem.. I created this folder structure:

Project_Folder/Assets/Resources/Tilesets/

Now i wanted to load a Tileset (a PNG File) from this folder via:

 Texture actualtex;
 
 [...]
 
 actualtex = Resources.Load("Tilesets/" + tilesetname) as Texture;
 tileset_size = actualtex.width;


And all i got is a NullReferenceException UnityEngine.Texture.get_width

I printed the actualtex, tilesetname..etc to the console. The "tilesetname" is absolute correct (!) and a File with that name exists in the known folder.

What am i doing wrong at loading this texture?

Ps.: I´m using C#

Comment
Add comment · Show 6
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 emrahsifoglu · Jun 26, 2013 at 06:56 PM 0
Share

Hi, try this one.

string texture = "Assets/Resources/Textures/name.png"; Texture2D inputTexture = (Texture2D)Resources.LoadAssetAtPath(texture, typeof(Texture2D));

avatar image Crashnorun · Sep 15, 2015 at 06:33 PM 0
Share

Hi All,

I'm running into a similar issue. I'm using Unity 5.1. I'm trying to load a sprite from the Resources folder. The folder structure is: Project Folder/Assets/Resources/North.jpg

When I call: Sprite North = Resources.Load("North") as Sprite;

North is null. I've double checked the spelling, and the location of the sprite. I've searched the forums high and low to find an answer. Nothing works as of yet. Any ideas would be great. Thanks

avatar image ArchVen · Jan 26, 2017 at 02:26 PM 0
Share

I have the same problem and still can't solve it. Here the method:

     public Sprite LoadSprite(string path)
     {
         return Resources.Load<Sprite>(path);
     }

I've tried lowercase, using back and forward slashes, using extension and not using it - not a chance((( It always returns null reference.

avatar image CarterG81 ArchVen · Jan 28, 2017 at 08:19 AM 0
Share
  1. Does your path include the filename but not the extension?

  2. Are you actually loading a Sprite, or a Texture2D? Does it return null if you load a different kind of asset like a Prefab? Have you tried loading it as an UnityEngine.Object (everything derives from Object)?

  3. Have you tried LoadAll? ($$anonymous$$aybe it is in a child folder?)

UnityEngine.Object[] loadAll$$anonymous$$yPrefabs = Resources.LoadAll(PrefabsPath);

avatar image CarterG81 CarterG81 · Jan 28, 2017 at 08:20 AM 0
Share

If it helps, this is how I handle Resources.Load. I do this at the start of the game (load EVERYTHING I need to, and store a reference to it).

 public string ObjectPrefabPath = "Prefabs/Objects/"; //This will get everything in "Assets/Resources/Prefabs/Objects" including contents in child folders.
 public static Dictionary<string, UnityEngine.Object> AllObjectPrefabs = new Dictionary<string, UnityEngine.Object>(); //This stores everything in a way that I can pick a single object, by name, very quickly.

 public void ArchiveAllObjectPrefabs()
     {
         UnityEngine.Object[] loadObjectPrefabs = Resources.LoadAll(ObjectPrefabPath); //Loads every single object in the folder.
 
         foreach (UnityEngine.Object prefab in loadObjectPrefabs)
         {
             string myObjectName = prefab.name;
             //Debug.Log("ArchiveAllObjectPrefabs: " + myObjectName); //Debugger
             AllObjectPrefabs.Add(myObjectName, prefab); //Stores the object by its prefab name.
         }
     }



Show more comments

5 Replies

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

Answer by emrahsifoglu · Jun 26, 2013 at 07:08 PM

Hi, try this one.

 string texture = "Assets/Resources/Textures/Turner.png";
 Texture2D inputTexture = (Texture2D)Resources.LoadAssetAtPath(texture, typeof(Texture2D));
Comment
Add comment · Show 3 · 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 CarterG81 · May 12, 2014 at 09:45 AM 1
Share

It's actually this

Resources.Load("Folder/Folder/Folder/File")

...or to be specific...

Resources.Load ("Textures/Turner");

this goes to your unity's Assets/Resources/Textures/Turner.png and loads it as Texture2D.

Wherever your resources folder is, you have to go there. It will NOT work if you do "Assets/Resources/Textures/Turner" but ins$$anonymous$$d you have to just do "Textures/Turner".

This is because it automatically assumes you are loading files from your Resources folder.

avatar image anisabboud · Mar 29, 2015 at 04:28 AM 1
Share

LoadAssetAtPath works in the Editor only. Use @$$anonymous$$ontraydavis's solution ins$$anonymous$$d.

"This function always return null in the standalone player or web player. This is useful for quickly accessing an asset for use in the editor only."

avatar image Bunny83 · Mar 29, 2015 at 05:17 AM 0
Share

-1

because it's it's not a solution since it's a method for in-editor use only like the docs explain.

avatar image
27

Answer by Montraydavis · Nov 16, 2012 at 12:39 AM

Make sure that your path does NOT include the .extension, in which your case, *.png .

 Resources.Load("Tilesets/PNGNameNoExtension");
Comment
Add comment · Show 5 · 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 Chaosgod_Esper · Nov 16, 2012 at 11:56 AM 0
Share

there is no extention.

Debug.Log(tilesetname);

gives me 'Hort_inner'

the filename is: 'Hort_inner.png

avatar image Montraydavis · Nov 16, 2012 at 12:34 PM 0
Share

try tilesetname.ToString(), and see if that makes any difference. Something in the string has to be wrong, if you are positive the contents exists in the specified folder.

 Resources.Load("Tilesets/" + tilesetname.ToString()) as Texture;

And maybe you can show me how you declared, initialized, and or altered tilesetname ?

avatar image DarkCooker · Feb 17, 2016 at 10:07 AM 0
Share

simple is beauty.

avatar image Noxury · Mar 24, 2016 at 01:00 PM 0
Share

And what if there are 2 or more files with the same name in the folder BUT different types like:

example.txt and example.png

avatar image Bunny83 Noxury · Mar 24, 2016 at 03:44 PM 0
Share

You should avoid that when possible. However the Load method has an overload which takes a System.Type parameter where you can state what type of asset you want. This should allow you to distinguish between Texture2D and TextAsset. However if you have one ".png" and one ".gif" with the same name you're in trouble. Same with a ".txt" and a ".xml".

The recent Unity versions also have a generic version of the Load method which you can use like this:

 Texture2D tex = Resources.Load<Texture2D>("example");

This should do two additional things:

  • filter for assets which are Texture2D only

  • automatically cast the return value to Texture2D

avatar image
3

Answer by KevinGelking · Jan 26, 2016 at 12:09 PM

Using a lowercase path solved the issue for me.

I had a file called "EE.png" under Resources (Assets\Resources\EE.png) It did not work for me to use this code:

 Resources.Load("EE",typeof(Texture2D)) as Texture2D

But it worked fine with a lowercase path/filename

 Resources.Load("ee",typeof(Texture2D)) as Texture2D

It may be worth a shot to try to lowercase the path before trying to access the file. The ".ToLower" string extension may be useful for that.

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
0

Answer by kapaakea2 · Apr 10, 2018 at 09:05 PM

as a note if there are special characters and you are on a Mac then you could encounter this issue with certain file names: https://forum.unity.com/threads/resources-load-with-special-characters-in-the-file-name-ios-and-mac.372881/

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 DanialKM · Sep 09, 2019 at 02:21 PM

try this I promise this would work { string address = Application.dataPath + "your local address"; or {string adress = "Resources/oil_splash.jpg"} byte[] byteArray = File.ReadAllBytes(@address); Texture2D pic = new Texture2D(12080,1024); bool check = pic.LoadImage(byteArray); if(check) print("done"); } i had same problem and finally i read bytes and then pass it to Texture2D

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

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

Related Questions

Resource folder resources loading 1 Answer

ridiculous load time using Resources.Load() on android build 0 Answers

Load texture and shaders at runtime on iPhone 2 Answers

Calling Resources.Load on a Texture is returning null 1 Answer

Loading textures into an array after build 1 Answer


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