loading texture from resources

I have the name of my texture (tester1.jpg)
I can copy this texture in any folder, now im trying in a resource folder in the Asset folder.
And, from the documentation, im doing this :

oObj.renderer.material.mainTexture = Resources.Load(“tester1”, typeof(Texture2D));

error:
Assets/NewBehaviourScript.cs(69,80): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Texture’. An explicit conversion exists (are you missing a cast?)

Any Idea? Any Hings?

I just want to find the texture i know the name in my Unity3d project folder and assign it to the material.

thanks

I have done similar to this in the past…

string fName = “”; ← i had this previously setup
fName = “Texture1”;
Texture2D tex = Resources.Load(fName) as Texture2D;

So i’m guessing you can then do your… oObj.renderer.material.mainTexture = tex;

Now, all my resources were .gif files and i can’t recall if i needed to specify .gif in the string name (fName)? (I’m away from my machine at the moment to check).

This may get you moving in a direction, hopefully it will be a forward one!

  • Matt.

oObj.renderer.material.mainTexture = Resources.Load(“tester1”, typeof(Texture2D)) as Texture2D;

The reason for that is that Resources.Load doesn’t know what it is you will be asking for at compile time, so it uses UnityEngine.Object as a return type, and boxes it up. Then mainTexture sees an Object, not realizing it internally is a Texture2D and cannot convert. Using the as will cast the Return from Load into a Texture2D so that mainTexture can be assigned properly.

I understand the “as” thing. But now, i still dont have my result. There’s more code for you guys. Its simple, but not working fine.
Im getting an object, changing the shader on the material and trying to assign a bumpmap texture in my ressources folder. no error, i got the new bump diffuse shader on the material, but cant change the bumpMap texture.

oObj = GameObject.Find( strObject );
MaterialRef = oObj.renderer.material;

MaterialRef.shader = Shader.Find(“Bumped Diffuse”);

Texture2D text = Resources.Load(“tester1”) as Texture2D;
MaterialRef.SetTexture(“_BumpMap”,text );

thanks

Is it because im not instanciate the new shader ?

My Bad guys… I had a problem in my Resources folder name. Thanks for the “AS” hint Ntero.