I have this code
GetComponent<MeshRenderer>().bounds
and this
GetComponent<Renderer>().bounds
Tried to search what is the difference between both components but didn't found any helpful thing.?
Difference between MeshRenderer and Renderer Component
There is MeshRenderer for displaying 3D Objects/models. There is also SpriteRenderer for displaying 2D images like sprites. The Renderer component is simply a base class that MeshRenderer and SpriteRenderer derives from. The bounds variable is declared in Renderer so accessing it from MeshRenderer or Renderer will give you the-same value. Because SpriteRenderer also derives from Renderer, you have access to other variables from the Renderer class.
So, GetComponent<MeshRenderer>().bounds and GetComponent<Renderer>().bounds are doing the-same things. The first one will get the MeshRenderer and access the bounds variable from the Renderer parent class. The second one will get the Renderer and access the bounds variable directly from it.
It's worth noting that you cannot attach Renderer to a GameObject. You can access it from a GameObject but cannot attach it.
For example, you can do this:
GetComponent<Renderer>()
but you cannot do this:
gameObject.AddComponent<Renderer>()
and will get the exception below:
Cannot add component of type 'Renderer' because it is abstract. Add component of type that is derived from 'Renderer' instead.
Only the components that derive from Renderer are what what can be attached to a GameObject. For example, SpriteRenderer and MeshRenderer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With