Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Molten-Path · Sep 09, 2017 at 01:44 PM · click objects

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");
             }
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Molten-Path · Sep 10, 2017 at 04:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges