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

Didn’t wanna access spriterenderer colour property directly so added a new public method in the ColorChanger class

public Color GetColour()
{
    return mySpriteRenderer.color;
}

Then added this nice ugly line in Hazard.cs

if (collision.gameObject.CompareTag("Block"))
{
    if (collision.gameObject.GetComponent<ColorChanger>().GetColour() == GetComponent<ColorChanger>().GetColour())
    {
        return;
    }
    ...
}
1 Like

Tis ugly
And just looking at it it doesnt self comment so dont really kow what is going on.

Better to put the line in its own method returning a bool so you the method name helps to self document the code.

ie If (IsColorMatch())…

Agree with what Eddie said above. At the very least, I’d assign those values in the comparison operator to their own variables.

ie. var collisionColor = collision.gameObject.GetComponent().GetColour();