- Home /
 
Material Resource will not load when Resources.Load is called
Hi,
I am trying to generate meshes to resemble planets using Primitive Spheres. However I am trying to load in a material that I have called barren_01.png using the material barren_01_planet. I have placed these files into a folder named Resources but when the game loads the texture that appears is pure purple (which I assume means the texture was not found). Upon debegging I found that the code where I set the material remains unchanged when using the Resources.Load call.
Why is this happening, what step am I missing?
 public class Planet {
 
 private const int MIN_PLANET_RADIUS = 10;
 private const int MAX_PLANET_RADIUS = 100;
 
 private PlanetType planetType;
 public string Name { get; set; }
 private int radius;
 
 public GameObject planetModel;
 
 /// <summary>
 /// Generates the planet.
 /// </summary>
 public void GeneratePlanet()
 {
     planetType = (PlanetType)Random.Range(0, System.Enum.GetNames(typeof(PlanetType)).Length);
     radius = Random.Range(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS);
     
     GeneratePlanetModel();
     
     Debug.Log(planetType.ToString() + " Planet Generated");
 }
 
 private void GeneratePlanetModel()
 {
     planetModel = GameObject.CreatePrimitive(PrimitiveType.Sphere);
     planetModel.transform.localScale *= radius;
     
     TexturePlanetModel();
 }
 
 /// <summary>
 /// Textures the planet based on its type
 /// </summary>
 private void TexturePlanetModel()
 {
     switch (planetType)
     {
     case PlanetType.Barren:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
         
     case PlanetType.Earth:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
         
     case PlanetType.Frozen:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
         
     case PlanetType.Molten:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
         
     case PlanetType.Gas:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
         
     case PlanetType.Moon:
         planetModel.renderer.material = Resources.Load("barren_01_planet") as Material;
         break;
     }
 }
 }
 
              Answer by chummscrubber · Jul 07, 2013 at 12:22 PM
Since I use a multi-level folder structure for textures all I needed was to do the following:
 case PlanetType.Barren:
         planetModel.renderer.material.mainTexture = Resources.Load("Textures/barren_01") as Texture;
         break;
 
              Answer by Stardog · Mar 15, 2014 at 03:54 PM
I think the actual solution is using:
 Resources.Load("material", typeof(Material)) as Material.
 
               The key being "typeof".
Your answer
 
             Follow this Question
Related Questions
One material for many objects with different textures 2 Answers
Skybox showing mostly white 0 Answers
load resources material 0 Answers
Assigning script-generated textures 0 Answers
Resources.Load(path+name) as texture == null reference 7 Answers