Can't assign Skybox/Cubemap material to sphere via script

  1. I want to assign material to Sphere dynamically (via script). However this code does not work:
[MenuItem("My Scripts/Test")]
public static void MatTest()
{
    string path = AssetDatabase.GetAssetPath (Selection.activeObject);  // auto2 Skybox/Cubemap material selected
    Material mat = (Material)Resources.Load (path);
    GameObject go = GameObject.Find ("TestSphere");
    go.GetComponent<Renderer>().material = mat;
}

I am able to manually add this material to sphere however.

  1. I also tried to change following line:
    Material mat=(Material)Resources.Load(path);

to:
Material mat=new Material(Shader.Find(“Skybox/Cubemap”));
mat=(Material)Resources.Load(path);

but it didn’t work either.

  1. Using sharedMaterial also does not work:
    go.GetComponent().sharedMaterial=mat;

2913572--214925--auto2.png

Problem solved. I changed line no 6 to:

Material mat=(Material)Resources.Load(“auto2”); // Just material name. No extension or path.