Help with raycasting?

I’m developing a game in which you have to find a button in a room, for the script i’m using this and just putting it on the button

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class OnClickLoadNextLevel : MonoBehaviour {
     void OnMouseUpAsButton() {
         SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex + 1);
     }
 }

but this creates some weird bug where you can’t be too close to the button to click it, and it sometimes takes a few clicks to find the point at which it works, i asked a question before and someone said something about raycasting, how do i make it so that from my mouse point a ray is sent out and if touching something with the tag of button and the left mouse key is pressed it changes level? If you wan’t to see the bug yourself go to ►Game Jolt - Share your creations

not sure if this will work.
make sure that your button has a collider and that its taged “Button”
then add this script to your player:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown (0)) {
			Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast (ray, out hit)) {
				if (hit.transform.tag == "Button"){
					hit.collider.gameObject.GetComponent<OnClickLoadNextLevel>().OnMouseUpAsButton ();
				}
			}
		}
	}
}

I hope this helps a bit

Is there a reason why you don’t use OnMouseDown for handling clicks?

Ray ray;
RaycastHit hit;

void Update()
{
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     if (Physics.Raycast(ray, out hit) && hit.transform.tag == "GameobjectTag")
     {
            if (Input.GetMouseButtonUp(0))
            {
                SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex + 1);
            }      
     }
}