CLONING COLORS QUEST: ‘Spikey Hazard’ - Solutions

Quest: Cloning Colors Quest
Challenge: Spikey Hazard

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

This is my solution to the Spikey hazard challange

using UnityEngine;

public class Hazard : MonoBehaviour
{
    private GameHandler gameHandler;

    void Start()
    {
        gameHandler = FindObjectOfType<GameHandler>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Block"))
        {
            Color hazardColor = this.gameObject.GetComponent<SpriteRenderer>().color;
            Color collisionColor = collision.gameObject.GetComponent<SpriteRenderer>().color;
            if(!collisionColor.Equals(hazardColor))
            {
                if (collision.gameObject.GetComponent<BlockMovement>().isActiveBool) // checking if the block is active in order to know if we need to change which block is active
                {
                    Destroy(collision.gameObject);
                    gameHandler.AllPlayerBlocksArrayUpdate();
                    gameHandler.DestroyedBlockUpdate();
                }
                else
                {
                    Destroy(collision.gameObject);
                }
            }
        }
    }
}
    void Start()
    {
        gameHandler = FindObjectOfType<GameHandler>();
    }
private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Block"))
    {
        if (collision.gameObject.GetComponent<BlockMovement>().isActiveBool)
        {
           // Adding a color comparism , if its not the same, change it
            if (collision.gameObject.GetComponent<SpriteRenderer>().color != GetComponent<SpriteRenderer>().color)
            {
                Destroy(collision.gameObject);
                gameHandler.AllPlayerBlocksArrayUpdate();
                gameHandler.DestroyedBlockUpdate();  
            }
        }
    }
}
2 Likes

I added this check collision.gameObject.GetComponent<SpriteRenderer>().color != this.GetComponent<SpriteRenderer>().color

In the hazard class

1 Like