- Home /
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
Answer by Lovrenc · Dec 05, 2013 at 03:27 PM
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 :)
Answer by GameVortex · Dec 06, 2013 at 10:51 AM
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.
Answer by JDDesigner · Jun 03, 2017 at 11:56 AM
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.
Answer by DanialKM · Sep 09, 2019 at 02:33 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 ,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
Your answer
Follow this Question
Related Questions
Does loading a Resource load that resource's dependencies? 1 Answer
Textures of different resolutions 1 Answer
Assigning a texture for object in inspector. Is it loaded right away? 0 Answers
Pink texture with Resources.Load 0 Answers
How can I load textures, sounds, and prefabs from users hard disk? 2 Answers