What is relation of "renderer" in SpriteRenderer?


I was reading some sample code and saw that people use “renderer” beside SpriteRenderer. I was under the impression that for 2D games, SpriteRenderer does 2D rendering and “renderer” is reference to material renderer of regular 3D textures.

I’m missing something here and would much appreciate if one would explain the difference to me.


renderer is the inherited member for the Renderer attached to the game object.

SpriteRenderer inherits from Renderer so renderer in 2D is the SpriteRenderer reference.

This way you use the same syntax whether in 2D or 3D. Ain’t that smart?

But since renderer is of type Renderer, if you need to perform some SpriteRenderer action, you need to cast the renderer variable:

SpriteRenderer sp = renderer as SpriteRenderer;