Search Unity

How can i make my game object not clickable?

Discussion in 'Scripting' started by brunoenvia, Oct 29, 2019.

  1. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    94
    i have this script where i click on my cubes and they turn to their respective color, but i can also change to the ball color and i dont want that. is there a way to make my ball unclickable? i've tried this so far but no success

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ChangeBallColor : MonoBehaviour
    7. {
    8.     [SerializeField] GameObject ball;
    9.     [SerializeField] Material ballMaterial;
    10.     [SerializeField] Material cubeMaterial;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Debug.Log(FindObjectOfType<ChangeBallColor>().gameObject);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23.  
    24.     private void OnMouseDown()
    25.     {
    26.         if (EventSystem.current.IsPointerOverGameObject() == false)
    27.         {
    28.             ballMaterial.color = cubeMaterial.color;
    29.         }
    30.  
    31.         if(ball)
    32.         {
    33.             return;
    34.         }
    35.     }
    36. }
    37.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You could use tags or some other way to detect the object type you want to interact with.
    And if you have a script that reacts to mouse clicks, don't add it to the ball?