How to check if the Canvas is infront of an object

I am still new to the UI GameObjects. I’ve liked them so far but I’m a little stumped on something. So I’m creating a game where NPC talk in real time using using Dialog boxes. I’ve made a Canvas UI with a RawImage and Text inside of it. This works great but the player can end up hidden behind these textboxes. My plan is to make a script that will that will check if the player or something important is behind a text box. If it is, display the textbox somewhere else. I can figure the second part on my own but the first part is what has me stumped. If it was just the player, I could just give every Canvas a box collider and have a raycast from the main camera to the player to see if it is blocking but I wouldn’t want it to hide enemies, projectiles, or other important items that may or may not exist. I could make a list of GameObjects with tags and make a for loop checking on every single one but that seems like a waste or processing power. I don’t know. Is there a better way to handle this?

Nevermind, I think I figured it out. Instead of having a script that searches through thousands of all important objects, I will give each important object a script that cast raycast to the camera to see if it is behind a text box.