[Solved] How to know if Sprite Mask is showing a sprite or not via script

Hi all, what I want to achieve is to know when a sprite is visible in the game scene using a sprite mask.

OnBecomeVisible() or renderer.isVisible does not seem to work. They are called right away or always set to true even though I am not showing them using a sprite mask.

What I want to get is to show specific enemies with my cursor, kind of like the Lens of Thruth in the Legend of Zelda, and that I have achieved. What I wanted to do now is do some other stuff(play sounds, particle effects, etc) at the moment the sprite becomes visible using my “Lens of Thruth”

I don’t find necessary to add any image or code since this question is pretty much straight forward, but if necessary I do it.

Any help with this will be much appreciated.

Hey @CesarCanto,

Have you considered using colliders/triggers?

Below is an example test script that demonstrates the idea of using colliders to detect when the sprite mask has revealed a sprite. The first screenshot shows the scene setup and sprite mask (“magic spotlight”) settings, and the second is the inspector for the sprite that is being revealed.




using UnityEngine;

public class MagicSpotlight : MonoBehaviour
{
    Camera cam;
    Transform camTransform;
    Transform spotlightTransform;

    void Start()
    {
        cam = Camera.main;
        camTransform = cam.transform;
        spotlightTransform = transform;
    }

    void Update()
    {
        var mousePos = Input.mousePosition;
        mousePos.z = -camTransform.position.z;
        var worldPos = cam.ScreenToWorldPoint(mousePos);
        spotlightTransform.position = worldPos;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log($"Magic Spotlight revealed {other.gameObject.name}");
    }
}

Hope this helps!

How are you toggling whether it is visable? if you just enable and disable the sprite renderer then you should be able to just use the below:

if(renderer == true)

provided you’re referencing the renderer correctly.