Why is it mandatory to get the entire Sprite Renderer component properties?

Greetings everybody,
there’s something that is unclear to me: why do you have to “get” the entire Sprite Renderer component, store all of its values in a variable and then change only its color instead of changing directly the object color?
Why can you normally change the Transform properties directly to move the car, but not the Sprite Renderer ones? Does it have something to do with the fact that Transform is always present and the other Components are optional?

Thank you
Alex

It is not mandatory. You can certainly “get” the component each time you want to set the color. It is, however, an expensive operation and it is sometimes better to grab a reference once and then use that instead. We do not actually “get” the entire Sprite Renderer component and store all of its values in a variable. What we get and store is a reference to the sprite renderer. That is really just a memory address that tells C# where in memory to find the all the information. The reason we don’t have to do that for a transform is because all game objects have transforms and Unity has already done the “getting” for us

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms