CLONING COLORS QUEST: ‘Create A Win Condition’ - Solutions

Quest: Cloning Colors Quest
Challenge: Create A Win Condition

Feel free to share your solutions, ideas and creations below. If you get stuck, you can find some ideas here for completing this challenge.

Just a basic solution for my win condition:

Created a new script and attached it to the flag object with the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlagWin : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Block"))
        {
            if (collision.gameObject.GetComponent<SpriteRenderer>().color == this.gameObject.GetComponent<SpriteRenderer>().color && collision.gameObject.GetComponent<BlockMovement>().isActiveBool)
            {
                    Debug.Log("You Win");
            }
        }
    }
}

Here is my solution.

I’ve modified the GameHandler.cs. There, I’ve created 2 variables and transformed the class into a Singleton.

public static GameHandler instance;
 private void Awake()
    {
        if (instance == null)
            instance = this;
        
    }
 public void WinCondition(int point)
    {
        winnerBlocksAmount += point;
        if(winnerBlocksAmount >= scoreToWin)
        {
            Debug.Log("Has ganado el juego");
        }
    }

Then, I’ve created a Flag.cs and attached it into the class.
Here is the code

public class Flag : MonoBehaviour
{
    [SerializeField] private int pointsForBlock = 1;

    private SpriteRenderer spriteRenderer;
    private bool flashingRoutineIsRunning;

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.GetComponent<BlockMovement>())
        {
            SpriteRenderer blockColor = collision.GetComponent<SpriteRenderer>();
            if (blockColor.color == spriteRenderer.color)
            {
                GameHandler.instance.WinCondition(pointsForBlock);
                Destroy(collision.gameObject);
            }
            else
            {
                if (!flashingRoutineIsRunning)
                {
                    StartCoroutine(FlashingRoutine());
                }
            }
        }
    }

    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private IEnumerator FlashingRoutine()
    {
        flashingRoutineIsRunning = true;
        Color initialColor = spriteRenderer.color;
        yield return new WaitForSeconds(.1f);
        spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, .5f);
        yield return new WaitForSeconds(.1f);
        spriteRenderer.color = initialColor;


        flashingRoutineIsRunning = false;
    }
}

Privacy & Terms