CLONING COLORS QUEST: ‘Magic Gem’ - Solutions

Quest: Cloning Colors Quest
Challenge: Magic Gem

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

Here is my solution to the second challenge.
If the block enters the trigger volume of the gem, I set the sprite renderer of the block to any value the gem has

using UnityEngine;

public class ChangeBlockColorByGem : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Block"))
        {
            collision.gameObject.GetComponent<SpriteRenderer>().color = this.gameObject.GetComponent<SpriteRenderer>().color;
        }
    }
}
1 Like

Getting its own color , then setting the collider to the same color.

    private SpriteRenderer _spriteRenderer;

    private void Start()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag($"Block"))
        {
            other.gameObject.GetComponent<SpriteRenderer>().color = _spriteRenderer.color;
        }
    }
2 Likes

Small refactoring. Is it ok to change the color of the block from the gem class?

public class GemChangeColor : MonoBehaviour
{
    private const string blockTag = "Block";

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag(blockTag))
        {
            GameObject player = collision.gameObject;
            player.GetComponent<SpriteRenderer>().color = this.GetComponent<SpriteRenderer>().color;
        }
    }
}
1 Like

If I remember this quest correctly, then yes, this should work fine. It makes sense to trigger the color change when the player collides with the gem and to do it from the gem class.

1 Like

Thank you!

1 Like