- Home /
Player click event got triggered when non player is clicked
So i have 2 script, the first script is just a player script, the second script is to generate the map, when i click the player, the log showed up, when i click the map, the log showed up, but when i clicked on nothing (area outside the map), log not showed up (which is good), so what i want is only by clicking the player the log showed up.
TileMap Script using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class TileMap : MonoBehaviour {
     
     public TileType[] tileTypes;
     int[,] tiles;
 
     int mapSizeX = 10;
     int mapSizeZ = 10;
 
     // Use this for initialization
     void Start()
     {
         //Allocate the tiles of the map
         tiles = new int[mapSizeX, mapSizeZ];
 
         //Create the map
         CreateMap();
 
         //Generate the map
         GenerateMap();
     }
     
     void GenerateMap()
     {
         for (int x = 0; x < mapSizeX; x++)
         {
             for (int z = 0; z < mapSizeZ; z++)
             {
                 TileType tt = tileTypes[tiles[x, z]];
                 Instantiate(tt.tileVisualPrefab, new Vector3(x, 0, z), Quaternion.identity);
             }
         }    
     }    
     void CreateMap()
     {
         //Create the map based on the tiles
         for (int x = 0; x < mapSizeX; x++)
         {
             for (int z = 0; z < mapSizeZ; z++)
             {
                 tiles[x, z] = 0;
             }
         } 
         //Create the road
         tiles[4, 2] = 1;
         tiles[4, 3] = 1;
         tiles[4, 4] = 1;
         tiles[5, 4] = 1;
         tiles[6, 4] = 1;
         tiles[7, 4] = 1;
         tiles[3, 4] = 1;
         tiles[2, 4] = 1;
         tiles[1, 4] = 1;
 
         //Create entry/ exit point
         tiles[4, 1] = 3;
     }
     // Update is called once per frame
     void Update()
     {
         
     }
 }
and the player script (player got capsule collider and the script attached)
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerScript : MonoBehaviour
 {
     public static bool isClicked;
 
     // 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))
             {
                 isClicked = true;
                 Debug.Log("Player CLicked");
             }
         }
     }
 }
Answer by TheSOULDev · Sep 09, 2017 at 02:19 PM
You will need to check the game object tags in the if: raycasting hits everything with a raycaster component. It does not differentiate what kind of game object it is, so you need to tell it to check the tags of a game object, and if the tag is the same as the tag you have on the player game object, only then should you execute some function.
it worked, i add the new line to check the tag
  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 Debug.Log("Ray check");
                 if (gameObject.tag == "Player")
                 {
                     isClicked = true;
                     Debug.Log("Player CLicked");
                 }               
             }
Thank you
Your answer
 
 
             Follow this Question
Related Questions
Click on two objects to render third 2 Answers
Hexagonal Grid equal distances 0 Answers
How to left click to rotate a box in a same direction in Unity 2D? 1 Answer
OnMouseDown() Basic Help 1 Answer
change scene on click of a 3d object 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                