Custom Editor - Cast target as GameObject?

I’m trying to access the MeshRenderer component from a Custom Editor, but I can’t seem to figure out the syntax in C#.

In JS what I’m trying to accomplish:

var mat : Material = target.renderer.material;

In C#, this is what I’ve got:

GameObject go = (GameObject)target;
Material mat = go.renderer.material;

However, Unity tells me that I can’t cast target as a GameObject.

This did the trick for me:

scriptName = (scriptName)target;	
go = (GameObject)scriptName.gameObject;

Have you tried using the ‘as’ keyword instead? Like changing “(GameObject)target” to “target as GameObject”?

Another thing you can try is casting it to a transform first:

Transform go = target as Transform;
Material mat = go.gameObject.renderer.material;

hope it helps,

Yoerick