How do I compare colors in an if statement?

Here’s the code:

public Color[] color;
    public int puyoColor = 4;
    public bool pieceRotate = true;
    public bool pieceFall = false;
    public PuyoScript puyoScript;
    public GameObject puyoSpawn;
    public SpawnPuyoScript spawnPuyoScript;
    int colorCount = 1;
    List<int> colors = new List<int>();
    public GameObject puyo1;
    void Start()
    {
        if (puyoColor == 3)
            this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 3)];

        if (puyoColor == 4)
            this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 4)];

        if (puyoColor == 5)
            this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 5)];
    }

    void Update()
    {
        if (pieceRotate == true)
            if (Input.GetKeyDown(KeyCode.X))
            {
                transform.eulerAngles += new UnityEngine.Vector3(0, 0, 90);
            }

        if (pieceFall == true)
        {
            transform.position -= new UnityEngine.Vector3(0, 0.001f, 0);
        }

        if (pieceFall == false)
        {
            transform.position -= new UnityEngine.Vector3(0, 0, 0);
        }

        if (color[colors.Count] = 4)
        { Debug.Log("Springtime!"); }
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.layer == 2)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer == 3)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer == 4)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer == 5)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer == 6)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer == 7)
        {
            pieceRotate = false;
            pieceFall = false;
        }

        if (other.gameObject.layer != 2)
        {
            pieceRotate = true;
            pieceFall = true;
            puyoScript.Fall = false;
            this.gameObject.tag = "Not Falling";
        }

        if (other.gameObject.layer != 3)
        {
            pieceRotate = true;
            pieceFall = true;
            puyoScript.Fall = false;
            this.gameObject.tag = "Not Falling";
        }

        if (other.gameObject.layer != 7)
        {
            pieceRotate = true;
            pieceFall = true;
            puyoScript.Fall = false;
            this.gameObject.tag = "Not Falling";
        }

        if (other.gameObject.tag == "Not Falling")
        {
            pieceFall = true;
            Debug.Log("Yes");
        }

        if (this.gameObject.tag == "Puyo 1")
        {
            pieceFall = true;
        }

        if (this.gameObject.tag == "Puyo 2")
        {
            pieceFall = true;
        }

        if (this.gameObject.tag == "Not Falling")
        {
            pieceFall = false;
        }
    }

You aren’t actually comparing colors BTW color is a List of int values. You’ve accidentally assigned the value not tested it.

if (color[colors.Count] = 4)

if (color[colors.Count] == 4)

Probably still early in the development cycle but you are being extremely verbose (needlessly) in many cases.

void Start()
{
    if (puyoColor == 3)
        this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 3)];

    if (puyoColor == 4)
        this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 4)];

    if (puyoColor == 5)
        this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, 5)];
}


void Start()
{
    this.gameObject.GetComponent<SpriteRenderer>().color = color[Random.Range(0, puyoColor)];
}

Colors are made of floats so you probably want to avoid comparing them for equality.

Instead, compare them for closeness to each other.

As @tleylan mentions, you may want to clean this up and use ScriptableObjects to configure it.

You can see an example in my Bitmap2Grid scene, specifically the B2GConfig class:

The above class lets you map colors to GameObjects.

Full project there to convert bitmaps into GameObjects in a grid based on the bitmap colors.

1 Like

How do I make a gameObject’s sprite = another gameObject sprite in a randomizer. For context, I’m making a tetris game and want to make a tetromino be the same as the oppentents when it shows up.

For even better context start with any one of the 27,000,000 Tetris tutorials already on Youtube.

My recommendation is to write the game by not writing the game… write the necessary parts of the game. Then assemble them when you have them working into the game. You want to test your theory and the code in a non-game environment. So for instance if you can’t currently change a sprite it doesn’t suddenly get easier when objects are moving or your are getting keyboard input.