Rendering & effects¶
Change how things look at runtime — tint renderers, swap material properties, drive lights and particle systems, and manage 2D sprites. Get the component with GetComponent<T>() (or a public field) and drive it.
Visuals are local
Color, light, and particle changes render on each player's client. A script that runs everywhere produces the same look for everyone — good for deterministic effects (a button that glows when pressed). Don't use a one‑client visual change to communicate authoritative state.
Renderer — tint & material¶
Renderer is the mesh's draw component. The quickest tint is SetColor/GetColor:
Renderer r = GetComponent<Renderer>();
r.SetColor(Color.red); // tint the material's main color
Color c = r.GetColor();
r.enabled = false; // stop drawing (object still exists)
Material m = r.material; // this instance's material (safe to modify)
material vs sharedMaterial
r.material gives this object's own copy — changing it affects only this object. r.sharedMaterial is the asset shared by every object using it — changing that re‑colors them all. Use material unless you intend a global change.
Material — shader properties¶
Drive any material property by name (matching the shader's property names):
Material m = GetComponent<Renderer>().material;
m.color = new Color(0.2f, 0.6f, 1f);
m.SetFloat("_Metallic", 0.8f);
m.SetColor("_EmissionColor", Color.cyan * 2f); // glow
float v = m.GetFloat("_Glossiness");
m.mainTexture = otherMat.mainTexture;
color · mainTexture · GetColor/SetColor(name, c) · GetFloat/SetFloat(name, f) · GetInt/SetInt(name, i).
Light¶
Light lamp = GetComponent<Light>();
lamp.enabled = true;
lamp.color = Color.yellow;
lamp.intensity = 2.5f;
lamp.range = 12f; // point/spot falloff distance
lamp.spotAngle = 45f; // spot cone
A pulsing lamp:
protected override void Update()
{
lamp.intensity = 1.5f + Mathf.Sin(Time.time * 3f) * 0.5f; // 1.0..2.0
}
enabled · color · intensity · range · spotAngle · type.
ParticleSystem¶
ParticleSystem fx = GetComponent<ParticleSystem>();
fx.Play();
fx.Emit(20); // burst of 20 right now
fx.Stop();
fx.Clear(); // remove live particles
bool done = fx.isStopped && fx.particleCount == 0;
Play · Stop · Pause · Clear · Emit(count) · isPlaying · isStopped · particleCount · time.
Sprites (2D)¶
SpriteRenderer draws a sprite in the world:
SpriteRenderer sr = GetComponent<SpriteRenderer>();
sr.color = Color.white;
sr.flipX = true;
sr.sortingOrder = 5; // draw order
sr.enabled = true;
SpriteRegistry is a helper for sprite‑indexed UI Images — card games, slot reels, inventory icons. Register sprites by index once, then swap an Image to any index:
SpriteRegistry.Clear();
SpriteRegistry.LoadFromResources(0, "Cards/ace_spades"); // index -> sprite from Resources
SpriteRegistry.LoadFromResources(1, "Cards/king_hearts");
SpriteRegistry.SetBack(cardImage); // show the "back" sprite (face down)
SpriteRegistry.SetImage(cardImage, 0); // reveal index 0 (ace of spades)
SpriteRegistry.HideImage(cardImage); // transparent, object stays active
SpriteRegistry.ShowImage(cardImage); // opaque again
LoadFromResources(index, path) |
register a sprite from Resources/ at an index |
SetImage(image, index) |
show that sprite on a UI Image |
SetBack(image) · HideImage · ShowImage |
back sprite / transparent / opaque |
Clear() |
drop all registrations (before re‑populating) |
Quick reference¶
| Group | Calls |
|---|---|
| Renderer | SetColor · GetColor · enabled · material · sharedMaterial |
| Material | color · mainTexture · SetColor/Float/Int(name,…) · GetColor/Float/Int(name) |
| Light | enabled · color · intensity · range · spotAngle · type |
| Particles | Play · Stop · Pause · Clear · Emit · isPlaying · particleCount |
| Sprites | SpriteRenderer.color/flipX/flipY/sortingOrder · SpriteRegistry.LoadFromResources/SetImage/SetBack/Hide/Show |
→ Use Math & vectors for the colors here, UI for screen images, and see SlotMachineGame.cs for SpriteRegistry in action.