- Home /
Loading texture from Resources folder not working
Hello all
So I want to load different textures for my game object when different conditions are met. Below is a snippet of one of the conditions: if ((amount > 151f) && (amount < 200f)) { Material.color = Color.blue; Renderer rend = GetComponent(); rend.material.mainTexture = Resources.Load("face2") as Texture;
}
This does not work and I do not understand why. Please assist; I am so frustrated with this right now
If you exactly copy pasted your code, then i think you made the mistake while using Renderer rend = GetComponent(); .As it gives a compiler error, i think you did not do that. So i think the problem is with loading of the texture itself. Just give a try to :-
Renderer rend = GetComponent<Renderer>();
Texture textureFace = Resources.Load("face2") as Texture;
rend.material.mainTexture = textureFace;
Now use debugger to check if the texture is loaded correctly. $$anonymous$$any times its the case of wrong path/name while loading from the resources
Hey Piyush
Thanks for your reply.
i put the code you sent into my code and it still doesn't work; not sure why
Also I am a Unity beginner and not really sure how to use the debugger.
If you get any error messages, could you post them here?
Sure, I will post them here if any should come up. Nothing has come up as yet; still do not know what's wrong with this
Answer by madks13 · Jul 25, 2018 at 09:34 AM
Resources.Load looks at resources in the Resources folder. If your resource is in a subfolder, you should add the relative path. For example, if your face2 is located at Resources/charcaters/faces/face2, you should use Resources.Load("characters/faces/face2").
@madks13 Thank you for your reply.
I just checked again to make sure and there are no subfolders in the resources folder Not sure why this code isn't working
what is the face2 resource you are trying to load?
Its a jpeg file, just a small picture I want to change the pictures on a sphere as each condition is met
Answer by Grey_Wolf9 · Jul 28, 2018 at 06:02 AM
This seems to work:
if ((amount > 151f) && (amount < 200f))
{
Renderer _rend = GetComponent<Renderer>();
Material _mat = _rend.material;
_rend.material.mainTexture = Resources.Load("face2") as Texture;
_mat.color = Color.blue;
}
Sorry, this was my mistake. After you posted the code, i understood what was wrong, which also was pretty obvious : you were simply setting the color before setting the texture. So each time you were basically setting the texture with default color. You should select this as the answer in case someone else has a similar problem so they can see the solution easier.
No problem. As i mentioned in point 3 : Even expert programmers make mistakes. The more experience and knowledge one has in program$$anonymous$$g, the easier it is to overlook the simplest errors, because of their obviousness. Thus the importance of debugging :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612171507im_/https://answers.unity.com/themes/thub/images/avi.jpg)