renderer.material quietly makes a copy.

The problem
One copy per enemy.
foreach ( var enemy in enemies ){ var r = enemy.GetComponent<Renderer>(); r.material.color = Color.red;}
The first read of .material clones the shared material for that renderer. 500 enemies, 500 copies.
Why it happens
material means: only this one.
Unity can't change one object while others share its material, so it gives that object a copy.
- renderer.material
- a copy for this object
- renderer.sharedMaterial
- the asset they all use
- The copies
- yours to Destroy
The fix in code
Changing them all? Change the shared one.
var shared = enemies[0] .GetComponent<Renderer>().sharedMaterial; shared.color = Color.red;
One material, one change, and every enemy that uses it turns red.
Watch out
sharedMaterial is the asset.
In the editor, a change to sharedMaterial is a change to the file. It stays after you stop playing.
- Changed in Play mode
- saved into the asset
- One object flashing
- .material is fine
- Many colors, no copies
- MaterialPropertyBlock
The rule
material copies. sharedMaterial shares.
- Just this one
- renderer.material
- All of them
- renderer.sharedMaterial
- Copies you made
- Destroy them