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

Here’s my gem script, fairly straightforward:

[RequireComponent(typeof(SpriteRenderer))]
public class Gem : MonoBehaviour
{
    ColorChanger colorChanger;
    SpriteRenderer spriteRenderer;

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

    private void OnTriggerEnter2D(Collider2D other) {
        if (other.gameObject.tag == "Block")
        {
            other.gameObject.GetComponent<ColorChanger>().SetColour(spriteRenderer.color);
        }
    }
}

I hadn’t actually bothered moving the player until this challenge, when I discovered jump wasn’t working at all. A quick look through the challenges and I couldn’t see anything about this so I guess there’s either a problem with my files or this is supposed to be a hidden challenge type deal?

Anyway I looked through BlockMovement.cs and found this line was the issue

if (!myRigidBody.IsTouchingLayers(LayerMask.GetMask("Foreground", "Block")))
{
    return;
}

I checked the Unity editor and didn’t even have Foreground or Block layers, so I create and assigned them accordingly. Now everything works fine :slight_smile:

Updated Fri Dec 10 2021 12:32

Oop should have paid more attention to the video where it clearly states to set the layers up like this :sweat_smile:

All good practice and you solved it!

1 Like

I do love a curt class. I definitely borrowed the CompareTag method and formatting from the solutions above, but otherwise, here’s mine in its entirety.

public class MagicGem : MonoBehaviour
{
    /// <summary>
    /// Cache the colors. Change only if needed.
    /// </summary>
    /// <param name="other">The triggering collider.</param>
    private void OnTriggerEnter2D(Collider2D other)
    {
        Color gemColor = GetComponent<SpriteRenderer>().color;
        Color blockColor = other.GetComponent<SpriteRenderer>().color;
        
        if (other.CompareTag($"Block") && gemColor != blockColor)
        {
            GetComponent<SpriteRenderer>().color = blockColor;
        }
    }
}

Privacy & Terms