- Home /
Load Material to GameObject
Hi there,
I'm trying to load a material that has a Bumpmap added to it. If it can't find the material for the object, then it'll look for just a basic Texture2D texture.
try { sphere.renderer.material = Resources.Load("Planets/" + PlanetType.ToString()+".mat") as Material; if (sphere.renderer.material == null) throw new Exception(); } catch (Exception) { sphere.renderer.material.mainTexture = Resources.Load("Planets/"+PlanetType.ToString()) as Texture2D; }
if (Texture != null)
{
sphere.renderer.material.mainTexture = Texture;
}
Now it's not working. It's just loading an empty material in there. I rechecked the material to make sure that I didn't save it incorrectly. But it shows that the textures are still there. I checked the Material property to make sure it not read only. but it isn't..
So what am I doing wrong here?? If this will not work, how would I go about doing this.
Files are :
- Acura.png = texture
- Acure_BM.png = bumpmap
- Acure.mat = material (including all bumpmap and information)
Thanks
Answer by Mike 3 · Aug 04, 2010 at 08:46 AM
The material load is wrong for sure - you can't add .mat on the end of it. If you have two items with the same name but different types, use the overload of Resources.Load which takes a second parameter for the type.
On top of that, you're currently assigning a null material to the renderer, throwing an exception, then using the null material as if nothing happened.
If it were me, I'd do something more like this:
Material mat = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Material)) as Material; if (mat != null) { sphere.renderer.material = mat; Texture2D tex = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
if (tex != null)
sphere.renderer.material.mainTexture = tex;
} else if (Texture != null) sphere.renderer.material.mainTexture = Texture;
That seems better, but it still seems to be doing the same thing, of loading empty materials to the spheres. If I comment out the texture2d part the material loads fine, if I comment out the material code, the texture loads fine... But just does not seem to like both. I'll be moving to making materials for all at some point. I just want to keep it so, if I havn't made a material, It'll fall on texture2d.
That other 'if (Texture != null)' is just so if I assign something in the unity editor, it'll overload the rest of this and just load what I picked in the editor. I should have left it out.
That should load both the material and texture, and use the fallback if the material couldn't be found
Answer by GeorgeAmos · Jan 28, 2014 at 01:43 AM
try
{
Material mat = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Material)) as Material;
if (mat != null)
{
sphere.renderer.material = mat;
}
else // mat is null, load the texture
{
Texture2D tex = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
if (tex != null)
sphere.renderer.material.mainTexture = tex;
}
}
catch (Exception)
{
sphere.renderer.material.mainTexture = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
}
Your answer
Follow this Question
Related Questions
Select colour then change texture 0 Answers
Material doesn't have a color property '_Color' 4 Answers
Can see 3D object through itself 0 Answers
How to make emissive maps glow? 2 Answers
Adding Texture image to gameobject 1 Answer