Change Scenes by Clicking Object

Hi! My name is Jason and I am having some trouble with changing scenes. The only question im asking is how do i change scenes by clicking an object. This is being used on my title screen and i have been googling answers for about two hours now. I thought I had it with onclick but i had a slight problem! You could click the entire screen and it would change frames! I want it to only change when you click on a certain object. I have seen RayCast but have learned that it doesn’t work and gives me at least 3 errors every time. If you have a fix, please give all the code, so I can learn where I was wrong and how I can improve.

Ey… Bruh… Check out… My Soundcloud…
https://soundcloud.com/cjcomputers/

Raycasting is definitely one way to do it, and might be better in some situations. However, one of the easiest ways that I have found to do this is to attach a script with an OnMouseDown() function to the object that you are trying to click on. That would end up looking something like this:

    private void OnMouseDown()
    {
        SceneManager.LoadScene("Scene name");
    }

Also, you will need to make sure that the item that you are trying to click on has a colider component for this to work.

If you still want to go about it using raycasting, the solution would probably be something like this in your update function:

        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                //Replace this with whatever logic you want to use to validate the objects you want to click on
                if(hit.collider.gameObject.tag == "Clickable")
                {
                    SceneManager.LoadScene("Scene name");
                }
            }
        }