Load 2D Textures with Resource.Load doesn't work

Hi,

i want to change a texture of an mesh if something happend in my script.
I use the following code for loading a jpg. das 2D Texture but it doesn’t happen.

Texture2D oil_fund;

void Start () 
{
oil_fund = (Texture2D)Resources.Load("Resources/oil_splash.jpg");
}

...

renderer.material.mainTexture = oil_fund;

Do You have an idea why my .jpg isn’t loaded ?
The picture is located at Assets/Resources/oil_splash.jpg

4 Answers

4

My guess: Since the function is “Resources.Load” i doubt that you have to type in “Resources/oil_splash.jpg” since it is already looking in resources folder. Also, image types (.jpg, .gif, .jpeg) are to be ignored. Try

Resources.Load("oil_splash");

Ignore this answer if you have Resources folder inside your Resources folder :slight_smile:

It's the same, doesn't work either :-(

Try setting the type you want:

oil_fund = Resources.Load("oil_splash", typeof(Texture2D)) as Texture2D;

If it does not work then double check your spelling of the file and the Resources folder. It has to be exact. It is case sensitive as well. So the Resources folder needs an upper case ‘R’.

Thanks but problem still exists... nothing happend again. only the attached texture via inspector disappears.

Well, there is not much more to it than this to load from Resources. Is your oil_splash.jpg imported as a texture or sprite or something else? Last thing would be to zip up your project and attach it here and I could take a look at it if you want.

Finaly late but not to late, the error was a misspelled filename in the script... sorry my fault. all answers are working then.

This was a great find. I had Resources.Load("Prefabs/units/Player") as Player and it wasn't working. As soon as I added typeof(Player), it worked.

Also make sure, that you do not enclude the filetype. So DON’T do this:

oil_fund = (Texture2D)Resources.Load("Resources/oil_splash.jpg");

But do this instead:

oil_fund = (Texture2D)Resources.Load("Resources/oil_splash");

I am not an expert and only started coding in C# a few moths ago, but when I was having this problem removing the filetype helped.

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 ,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
,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