- Home /
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#
Hi, try this one.
string texture = "Assets/Resources/Textures/name.png"; Texture2D inputTexture = (Texture2D)Resources.LoadAssetAtPath(texture, typeof(Texture2D));
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
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.
Does your path include the filename but not the extension?
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)?
Have you tried LoadAll? ($$anonymous$$aybe it is in a child folder?)
UnityEngine.Object[] loadAll$$anonymous$$yPrefabs = Resources.LoadAll(PrefabsPath);
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.
}
}
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));
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.
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."
-1
because it's it's not a solution since it's a method for in-editor use only like the docs explain.
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");
there is no extention.
Debug.Log(tilesetname);
gives me 'Hort_inner'
the filename is: 'Hort_inner.png
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 ?
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
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
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.
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/
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