How to check whether a material is unique?

Hello everyone!

Hopefully this one will be quite easy. On game start I instantiate a bunch of objects on the scene. They are ready prefabs with materials. Then I would like to differentiate them a bit by drawing additional textures to them. I started with something like this:

void Update () {

		if (!Input.GetMouseButton (0))
			return;

		RaycastHit hit;
		if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition),out hit))
			return;

		Renderer renderer = hit.collider.renderer;
		MeshCollider meshCollider = (MeshCollider)hit.collider;
		if (renderer == null || renderer.sharedMaterial == null ||
		    renderer.sharedMaterial.mainTexture == null || meshCollider == null)
			return;

		//if( rendered.material is not unique (was not cloned yet))
		{
			renderer.material.mainTexture = (Texture2D)Instantiate(renderer.material.mainTexture);
		}

		Texture2D tex = (Texture2D)renderer.material.mainTexture;
		Vector2 pixelUV = hit.textureCoord;
		pixelUV.x *= tex.width;
		pixelUV.y *= tex.height;
		Color [] c = new Color[100];

		for(int i=0; i<100; i++)	{	c *= Color.red;	}*
  •  tex.SetPixels((int)pixelUV.x-5,(int)pixelUV.y-5, 10,10, c);*
    
  •  tex.Apply();*
    
  • }*
    This script is pretty straightforward, but what happens is that it keeps on instantiating new textures every frame. I need a way to know that certain material has already been unlinked from the shared materials and is free to be manipulated individualy.
    Do you guys have any ideas? Thanks in advance!
    John

You don’t need to Instantiate() a new material. Any time you make any material change (at runtime), Unity replaces the shared material with a new material instance. So change away and you will be okay.