4.6 GUI - Press New GUI Button Without Raycasting!

Hello,

I’ve beaten my head against the wall long enough with this one before I asked for help. I’ve been playing with 4.6 beta for about a month now, and I have the new GUI all figured out.

However, I have a prefab which is instantiated upon a raycast. I can’t seem to find anywhere on the new GUI to prevent the GUI buttons from triggering a raycast, but still pressing the button!!!

Here’s my code, if it helps.

		if(Input.GetTouch(0).phase == TouchPhase.Began)
		{
			Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
			RaycastHit hit;
			if (Physics.Raycast (ray, out hit, 5)) 
			{
				if(hit.collider.gameObject.tag == "Fish")
				{
				Debug.Log("your code sucks");
				Quaternion q = Quaternion.FromToRotation(-transform.forward, hit.normal);
				Instantiate (inventoryControl.activeBomb, hit.point+hit.normal*0.01f, q);
				}
			}
		}

I should note that I have tried setting both a tag and layer control on the raycast to only allow it to function on gameobjects with the associated properties. But it still raycasts through the button!!!

You would think this would be easier.

Any help is appreciated.

I am slightly confused about your terminology… I assume the following:

  • “instantiated upon a raycast” means “You do a physics raycast and instantiate something at the point where the ray first collides”
  • “X is triggering a raycast” means “the ray collides with a physics collider of X”

Right?

And your problem is, that your ray does not collide with an Unity UI button you placed?

Have you checked whether you have created a collider on your UI button? That is not the default for UI buttons. (The new UI does not use Physic to detect UI hits, i.e. like NGUI does)

And have you checked that your button is in the correct world space position? (Check with the SceneView). The default for Unity UI is to have a screen space overlay with an own UI camera. So your button might be at a total different world-position and still appear to be spot-on in your game.

Oh and one more: Are you sure you want to do a Physics raycast in the first place? If you only want to have the UI control that is under the mouse right now, then it is much more appropiate to use EventSystem.RaycastAll

So for future reference if anyone else happens to find this page. I ended up just creating a rectangle which allows the raycast to only cast through it. I turn it off when the GUI is active. When the GUI is inactive, the rectangle is laid out so it does not overlap my GUI buttons.

private Rect rectiggy = new Rect(0,Screen.height * .35f,Screen.width,Screen.height * .5f);

private int touchNumbers = 0;

		if(rectiggy.Contains(Input.GetTouch(v).position))
		{
			if(Input.GetTouch(touchNumbers).phase == TouchPhase.Began)
			{
				Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(dick).position);
				RaycastHit hit;
				if (Physics.Raycast (ray, out hit, 5)) 
				{
					Quaternion q = Quaternion.FromToRotation(-transform.forward, hit.normal);
					Instantiate (inventoryControl.activeBomb, hit.point+hit.normal*0.01f, q);
				}
			}
		}

PLEASE IGNORE my variable names. I have a wierd variable naming system so I can remember their function.

Hey. Sorry I missed your original question. But the answer is pretty simple.

In the 4.6 system you don’t need to do your own raycasting against physics colliders. Simply add a graphics raycaster to the camera, and implement the appropriate interfaces on your components. The event system will automatically take care of blocking clicks on UI elements for you.

For a video demonstrating the principle check out here.