How do I let the player interact with objects close to them?

Hello everyone! I’ll try to keep it brief and get to the point.
I have a little scene for school that I’m trying to put a little extra effort into. Alongside a few OnTriggerEnter effects I have to include, I wanted a few objects that are interactive when the player clicks on them.

Specifically I have a lightswitch and a box. I’m trying to let the box play an audio file when the player clicks on it, and that I pulled off succesfully. However that’s not good enough! You can be anywhere in the scene and trigger the sound as long as you can see the box, and that’s a bad thing to me.

I’m trying to get it so that the player, when close by (less than 2 meters probably) sees a prompt on OnMouseOver, and then when the player clicks on the box, the audio file plays. The only documentation I could find on this kind of input (only when the player is close to the GameObject) was in Js (that didn’t even work) while I work in C#.

If you could help me figure out how to fix the box, that should give me enough information to fix the lightswitch on my own, though I had one question about that as well: can I, when the player clicks on the lightswitch, change it to a ‘lightswitch_off’ object that would still be able to be reverted back when the player clicks again, or should I make it play an animation?

You could use raycast.

You can set the distance of the raycast, so if it hits object, you know it is close enough. Now if that object is one you can interact with, you can popup a note to notify the user he can interact with it, and when he clicks do your action.

About the light switch on / off operation. Its is best if your light switch handles this. When user clicks, simply tell your switch, user clicked on you. And it should know “ok, i am on, i should turn off now”.