I want to be able to easily swap the colour scheme on my game using code, rather than having a bunch of different versions of each sprite. All I want to do is tint my sprites, using their material.
When I change the ‘Tint’ of the material using the inspector, it works fine, tinting the sprites. But when I try to change it in code, it just colours the whole bounding box of the sprite 1 solid colour.
I’m new to using C#, so it may well be just me making a dumb mistake.
I think the issue might be the “_Color” in the SetColor code. I’ve seen in other posts, that it should be “_TintColor”, but when I use that Unity just tells me the material doesn’t have a _TintColor.
sprite.sharedMaterial.SetColor ("_Color", newColour);
Here’s my whole script, in case I’ve made some other stupid error in it that causes this.
public ColourSchemes parent;
private string currentColours;
private Color newColour;
private Renderer sprite;
void Start ()
{
sprite = gameObject.GetComponent<Renderer> ();
currentColours = "Unset";
}
void Update ()
{
//check for colour scheme change
if (currentColours != parent.currentColours)
{
if (parent.currentColours == "Default")
DefaultColours ();
if (parent.currentColours == "Midnight")
MidnightColours ();
//set colour
sprite.sharedMaterial.SetColor ("_Color", newColour);
}
}
void DefaultColours ()
{
newColour = new Color (0xFF, 0xFF, 0xFF, 0xFF);
currentColours = "Default";
}
void MidnightColours ()
{
newColour = new Color (0xFF, 0x00, 0x00, 0xFF);
currentColours = "Midnight";
}
‘parent’ is the script that handles which colour scheme is active, this script just sets one of the colours.
‘currentColours’ is to hold what colour scheme is active in THIS script. I use it to compare to the parent script.
'newColour is purely for clarity of reading the script. I know I could set that directly in the ‘SetColor’ code, but this makes it easier for me to edit at a later date.
‘sprite’ is my renderer, and as my colours ARE changing, just not the way I want, I guess this is working correctly.