How to only click on certain objects/buttons and null the rest please help!!
Hey everyone, I was wondering if anyone could help me with this. I have been trying to figure it out for days. I would really appreciate the help. I am trying to figure out how to null/not have click activated when I click on the pop up UIs. The pictures as shown:
I have a script in which I am able to click on the triangles. The blue space is nulled out/does not do anything. The code I am using is:
public class ClickObject : MonoBehaviour {
[SerializeField]
private GameObject NextLevelUI;
[SerializeField]
private GameObject TryAgainUI;
void Update (){
RaycastHit2D hit;
if(Input.GetMouseButtonDown(0)){
hit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero);
if (hit.collider != null){
TryAgainUI.SetActive(true);
if(hit.collider.GetComponent<MoveSample>()!= null && hit.transform.gameObject.tag=="TriangleP"){
NextLevelUI.SetActive(true);
TryAgainUI.SetActive (false);{
Debug.Log("SUCCESS");
When I click on the right object that has the movesample script attached, It directs to the correct pop up UI. When I clicked on the wrong object, it goes to the incorrect pop up. Here is an example of the correct UI:
What I want to do is have the ability to ONLY click on the button and nothing else. The triangles and background all null/unable to do anything. I have been looking into bools, event systems, if else statements, but not sure how to do this. If anyone could help I would GREATLY appreciate it. Thank you.
Answer by FWCorey · Apr 23, 2016 at 04:07 AM
Create layers for the objects you want your raycasts to hit and when you perform your:
hit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero);use instead:
hit=Physics2D.Raycast( Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, LayerMask.GetMask("MyValidLayerName"));
You can find info on using Layer tags here - Tags and Layers
@FWCorey would this solve the issue? I typed in exactly what you put under the "use ins$$anonymous$$d", replacing the $$anonymous$$yValidLayerName with "UI", as that are with my pop ups. It does not seem to work, I am still able to click on the triangles which register the incorrect pop up UI while being in the correct pop up UI. Am I missing something or not quite understanding? Anymore help or understanding would be greatly appreciated!!
UI is one of Unity's built-in layers. You need to create a new layer of your own and have only the GameObjects you want clickable on it.
@FWCorey thanks for replying. I did create my own UI layer, applied it to the pop up UI and had that code above, but I am still able to click on the triangles. Am I missing something? Any help would be greatly appreciated. Thank you.