How to make an if statement asking for the material name of an GameObject (C#)

As an beginner I think I made a simple mistake but i can´t solve it!
My code always returns the else statements “Does not work!” and not as required “Works!” although
it does write the right name “Test_rot” of the variable matName in the Console.

void Start() {

		GameObject obj = GameObject.Find ("Test_Box");
		Debug.Log ("Game object is " + obj);

		string matName = obj.GetComponent<Renderer>  (); 
		Debug.Log (matName); 
		if (matName == "Test_rot") 
				Debug.Log ("Works!");

				Debug.Log ("Does not work!");


When you write this:

obj.GetComponent<Renderer>  ().material

Unity creates a copy of the material, and uses that for editing. This is because materials are shared, so if you had edited the material directly, every object with the same material would be changed. Read more details here.

The shared material has the name “Test_rot(Instance)”, as you can see in your console.

If you don’t want to edit the material, use renderer.sharedMaterial instead - that keeps a reference to the original material.

Otherwise, you’ll have to edit the name of the newly created material:

string originalMatName = obj.GetComponent<Renderer>  (); 

//create new material with .material, and assign the old name
obj.GetComponent<Renderer>() = originalMatName;