Why can't "GetComponent SpriteRenderer ().color" be modified from the script?

I was able to use GetComponent<Renderer>().material.color to change the tint of a sprite, but the problem is that it adds a draw call.

I tried the following:

mySpriteObject.GetComponent<SpriteRenderer>().color = new Color(0,0,0,255);

The tint of the sprite doesn’t change if I do it from script. But it changes if I modify “color” from the inspector.

Is there any reason why I can’t use GetComponent().color to change the tint of a sprite?

EDIT: For some reason, the website removed the <SpriteRenderer> and <Renderer> so it doesn’t show my question properly

the color values are floats with a range of 0 to 1. To create a red tint would be:

mySpriteObject.GetComponent<SpriteRenderer>().color = new Color( 1, 0, 0, 1 );

You can, but you’re going to want to use

myGameobject.GetComponent<SpriteRenderer>().Color = new Color(r,g,b);

Make sure that it knows its a SpriteRenderer, and not just a regular renderer. The transform.renderer property is not the good way to access your objects specific renderer, because there are all different kinds of renderers.

If it still isn’t working, you’re going to want to check your sprite to see if it has white or gray colors in the image, because sometimes if you have a non-white / gray sprite, it’s going to look weird when you tint it.