CLONING COLORS QUEST - Spikey Hazard

Here it is my solution:
I’ve created a public property called MyColor so I can compare if it matches with the spikes.

Hazard.cs code:

public class Hazard : MonoBehaviour
{
    private GameHandler gameHandler;
    private ColorChanger colorChanger;

    private void Awake()
    {
        colorChanger = GetComponent<ColorChanger>();
    }
    void Start()
    {
        gameHandler = FindObjectOfType<GameHandler>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Block"))
        {
            if(colorChanger.MyColor == collision.GetComponent<ColorChanger>().MyColor) { return; }

            if (collision.gameObject.GetComponent<BlockMovement>().isActiveBool)
            {
                Destroy(collision.gameObject);
                gameHandler.AllPlayerBlocksArrayUpdate();
                gameHandler.DestroyedBlockUpdate();
            }
            else
            {
                Destroy(collision.gameObject);
            }
        }
    }
}

ColorChanger.cs code:

public class ColorChanger : MonoBehaviour
{

    public enum ColorOptions
    {
        Red = 1,
        Yellow = 2,
        Blue = 3
    }

    private SpriteRenderer mySpriteRenderer;
    [SerializeField] private ColorOptions myColor;

    public ColorOptions MyColor { get { return myColor; } set { myColor = value; } }

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

    private void Start()
    {
        //Por default inicializa el enum en 0 si no esta seleccionado desde el Inspector
        if (myColor == 0)
        {
            ChangeColor(GetRandomEnumNumber());
            
        }
        else
        {
            ChangeColor((int)myColor);
        }
    }

    private int GetRandomEnumNumber()
    {
        int randomNumber = Random.Range(1, System.Enum.GetValues(typeof(ColorOptions)).Length + 1);
        return randomNumber;
    }

    public void ChangeColor(int colorNumber)
    {
        switch (colorNumber)
        {
            case (int)ColorOptions.Red:
                mySpriteRenderer.color = Color.red; 
                MyColor = ColorOptions.Red; break;
            case (int)ColorOptions.Yellow:
                mySpriteRenderer.color = Color.yellow;
                MyColor = ColorOptions.Yellow; break;
            case (int)ColorOptions.Blue:
                mySpriteRenderer.color = Color.blue;
                MyColor = ColorOptions.Blue; break;

        }
    }
}

Privacy & Terms