Change material with EditorScript

I try to change the material of a gameobject in the editor by a EditorScript.
I use this code:

	if(boolean == true)
	{
		var newMat = AssetDatabase.LoadAssetAtPath("Assets/blue.mat", typeof(Material));
		if(newMat != null)
          	Debug.Log("Asset loaded");
        else
        	Debug.Log("cant find asset");
        	
		target.renderer.material = newMat;

	}

when i click the boolean in the EditorScript my debug log prints “Cant find asset”.

Sorry, I assumed you were using C# because of the unfamiliar code I saw as the second parameter of your call to LoadAssetPath().

When I use LoadAssetAtPath() I use it like this:

var newMat = AssetDatabase.LoadAssetAtPath("Assets/blue.mat", Material);

I’m not sure if that’s the problem or not. I would give it a try both ways. I don’t have a machine with Unity handy, but I’m curious to see the output of:

Debug.Log(typeof(Material);
Debug.Log(AssetDatabase.GenerateUniqueAssetPath());

It works now! i used the code like this:
Make sure you begin with “Assets” in the path.

@CustomEditor (scriptname)

class NewBehaviourScript extends Editor{

function OnInspectorGUI()
{
var thePath = “Assets/ExampleMaterial.mat”;
var loadedMaterial = AssetDatabase.LoadAssetAtPath(thePath, Material);

Debug.Log("Just loaded this material: " + loadedMaterial.name);
target.renderer.material = loadedMaterial;

}
}

Thanks Jahroy

Do you use?

Material SRm = (Material)AssetDatabase.LoadAssetAtPath("Assets/MyMaterial.mat", typeof(Material));