Problem with obtaining material from other GameObjects.

I have a scene where the Player obtains the material from other GameObjects on pressing key ‘E’.

    public Renderer rend;
	public Material defaultMaterial;
	private float distance = 3;

	void Update()
		Ray ray = new Ray(transform.position + transform.up * 1, transform.forward);
		RaycastHit hit;

		Debug.DrawRay(transform.position + transform.up * 1, transform.forward,;

		if(Physics.Raycast(ray, out hit, distance))
			if(hit.collider.tag == "Enemy")
				Renderer otherRend = hit.collider.GetComponent<Renderer>();
				if(Input.GetKeyDown(KeyCode.E) && otherRend.material != defaultMaterial)
					rend.material = otherRend.material;
					otherRend.material = defaultMaterial;

After the player obtains the material from the object, the material of that object is set to default material which I can specify within Inspector. Once that material is set to default, the Player should no longer be able to get the material of that object. In short, I don’t want player to get default material, but if its not default, then it should be able to get it. That’s why I wrote the condition so that it should check if otherRend has default material or not.

if(Input.GetKeyDown(KeyCode.E) && otherRend.material != defaultMaterial)

But its not working as Player obtains the material, no matter what. What changes can be made? Any
help is appreciated.

As stated in the API docs, assigning to .material will usually clone the material. That’s why even if you do

Renderer1.material = Renderer2.material;
if (Renderer1.material == Renderer2.material)

will usually not Log anything. Renderer.sharedMaterial shouldn’t have this problem.
Just remember if you then modify the shared material anywhere it will change on all objects that use that instance of the material.