Color Swapping

So I have 3 sprites.

1 is player, which is a white square.

2 is the ground/level, which is black

3 is a flag to finish the level

The background is default to white.

When you press “S” , you change black, and the floor changes to white.

You can never see the player or the level simultaneously, which is the main mechanic in this game.

This script will change me from black to white, but not to any other object except for the flag (These are all sprites btw)

It for whatever reason will not change color when I want it too. Only the player and the flag.
I have 2 separate scripts. They are both the exact same, except flipped so you are never the same color as the level. 1 is on the player, and 1 should be on the level sprite.

Any thoughts?..

Code attached to player:

using UnityEngine;
using System.Collections;

public class ColorSwitch : MonoBehaviour
{
    private bool colorSet = true;



    void Start ()
    {
     
    }


    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.S))
        {
            colorSet = !colorSet;
        }

 
        if (colorSet == true)
        {
            gameObject.GetComponent<SpriteRenderer> ().color = Color.white;
            Debug.Log ("PLAYER should be white!");
        }

        else if (colorSet == false)
        {
            gameObject.GetComponent<SpriteRenderer> ().color = Color.black;
            Debug.Log ("PLAYER should be black!");
        }

    }
}

Code attached to level:

using UnityEngine;
using System.Collections;

public class LevelColorSwitch : MonoBehaviour
{
    private bool colorSet = true;



    void Start ()
    {

    }


    void Update ()
    {
        if (Input.GetKeyDown (KeyCode.S))
        {
            colorSet = !colorSet;
        }


        if (colorSet == true)
        {
            gameObject.GetComponentInChildren<SpriteRenderer> ().color = Color.black;
            Debug.Log ("PLAYER should be white!");
        }

        else if (colorSet == false)
        {
            gameObject.GetComponentInChildren<SpriteRenderer> ().color = Color.white;
            Debug.Log ("PLAYER should be black!");
        }

    }
}

What?

Maybe use GetComponentsInChildren<> instead, if you player has many SpriteRenderers inside it?

Setting a black sprite to white will not make it white! If a sprite renderer’s color is set to white, the sprite will appear as its natural color, which is, in the case of the level sprite, black. If a sprite renderer’s color is set to black, then the sprite will appear… black.

Just set the alpha channel to 0 to make it transparent instead, then back to 1 to make it opaque again.

1 Like

Make all your sprites white, then tint them with the SpriteRenderer.

I just whipped this up, and haven’t tested it, but something like this can be reused on any objects that need to swap between two colors on a keypress:

using UnityEngine;

// this assumes a sprite renderer is on the same object as this component
public class ColorSwap : MonoBehaviour
{
    [Tooltip("The starting color for this object.")]
    public Color primary;

    [Tooltip("The secondary color that will be swapped to and from.")]
    public Color secondary;

    [Tooltip("The key used to swap colors.")]
    public KeyCode keyToSwap;

    private SpriteRenderer spriteRenderer; // this object's sprite renderer
    private bool isPrimary; // represents which color is currently applied

    // called once by Unity when the game first loads
    private void Awake()
    {
        // get the sprite renderer
        spriteRenderer = GetComponent<SpriteRenderer>();

        // set its color to the primary to start
        spriteRenderer.color = primary;

        // initialize the color state
        isPrimary = true;
    }

    public void SwapColors()
    {
        // toggle the color state
        isPrimary = !isPrimary;

        // apply the color based on the new color state
        spriteRenderer.color = isPrimary ? primary : secondary;
    }

    // called by Unity every frame
    private void Update()
    {
        if(Input.GetKeyDown(keyToSwap))
        {
            SwapColors();
        }
    }

    // called by Unity when you change the inspector for this component
    private void OnValidate()
    {
        // preview the primary color applied to the sprite renderer
        GetComponent<SpriteRenderer>().color = primary;
    }
}

If you decide to use this code, let me know if it works for you and if you need more explanation about it.