Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by slake · Apr 07, 2015 at 04:30 AM · sprite2d gamemouse click

How to detect mouse click on sprite?

Hi guys! Sorry if my question is very simple, but this confused me and I just want understood how unity works with sprites. I have very simple project:

Project contains GameObject "Board" with controller:

 public class BoardController : MonoBehaviour {
     public int boardRowCount;
     public int boardColumnCount;
     public GameObject cellPrefab;
 
     void Start()
     {
         for (var row = 0; row < this.boardRowCount; row++)
         {
             for (var column = 0; column < this.boardColumnCount; column++)
             {
                 Instantiate(cellPrefab, new Vector3(row, column, 0), Quaternion.identity);
             }
         }
     }
 }


and Prefab Cell with controller:

 public class CellControler : MonoBehaviour
 {
     void Update () {
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log("Click");
         }
     }
 }

This code build next simple table:

alt text

My questions next:

  1. Why when I click on the cell it write in console window "Click" 9 times (depends of cell count).

  2. What best way to detect on which cell was clicked.

myimg.png (41.1 kB)
Comment
Add comment · Show 1
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 slake · Apr 07, 2015 at 12:48 PM 0
Share

Thanks guys! Very helpful)

2 Replies

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

Answer by Mehul-Rughani · Apr 07, 2015 at 05:04 AM

1) It Displaying "Click" 9 times Because You have assigned Cellcontroller script to each sprite that are instantiated.so click event will be called for every sprite...

2) Add collider to sprite prefab and assign one script to it with OnMouseDown() method and write code u want on click event

Comment
Add comment · 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
3

Answer by yashpal · Apr 07, 2015 at 05:16 AM

hello @slake ,

1) Why when I click on the cell it write in console window "Click" 9 times (depends of cell count)?

ans :- Input.GetMouseButtonDown(0) is just detect that mouse button is down (means click any where in screen ). it don't depend on mouse click on particular sprite.

2) What best way to detect on which cell was clicked.

Ans:- you just attached collider to your Prefab Cell(ex. boxCollider2D, but you can attach any collider. ). and just use OnMouseDown to detect touch on that particular cell.

here are 2d game learning videos.

here are scripting learning videos.

if you use unity 4.6 or 5. you can use events to detect touch (for that you want to use canvas). here is the new UI learning videos.

I am highly recommend you to check out those videos.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pivot on sprite 2D not changing 1 Answer

Sprite Mask that affects only the childrens of a GameObject 5 Answers

Rotating a 2D sprite(Ring) in a fixed position 0 Answers

HOW TO MAKE A 2D OBJECT MOVE WITH TOUCH INPUT ON ANOTHER 1 Answer

Layer Sprites Based on Y axis 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