How do I switch scenes by pressing e on an object?

Basically the title but i already have on that works but i can press e at anytime and switch scenes. How can i press e only on an object to switch not just anywhere?

Heres what i have currently`

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class loadScene : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
            SceneManager.LoadScene("DemoSceneFree");
    }
}

Well to do this you need to decide what it means to be “only on an object” I’m going to assume you mean, only when the mouse cursor is over an object? If that is the case then I would advise two mechanisms.

The first, make sure your loadScene object has a collider on it, then perform a raycast from the mouse screen position through the main camera into world space to see if it is over the loadScene object,

Alternatively to avoid doing the raycasts yourself you could still put a collider on the object and then use the OnMouseOver method of MonoBehaviours and OnMouseExit to detect the mouse entering and exiting the object, in these methods you could set a flag for example “isMouseOver” on the object, then change your if statement to include a check on that flag and only respond if it is true. The documentation for OnMouseEnter event can be found here Unity - Scripting API: MonoBehaviour.OnMouseOver()

Another thing that you may wish to incorporate is a distance check from the player to the object, as both of these methods will without restriction let you detect the mouse being over the object from much further away than you may want.

Hope this helps lead you in the right direction.