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 /
This question was closed Jun 26, 2017 at 01:48 PM by nedas60 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nedas60 · Jun 26, 2017 at 09:17 AM · unity 2ddetectionmouseclick

Clicking on one object activates other objects

I am trying to create a Minesweeper + clicker/idle game for practice. For now, I can't get the clicking to work properly. I spawned in all objects through code meaning they are all the same prefabs but at different locations. When I click them the code activates for all of them and all of the tiles change to being open. I am not sure if I'm using the wrong click function here, as I know there is also an OnMouseDown() function instead of Input.GetMouseButtonDown(0) but the only one where the code activates is in the second one.

 public class TileClick : MonoBehaviour {
 
     public GameObject openTile;
     
     // Use this for initialization
     void Start () {    
     }    
 
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0)) {
             Instantiate(openTile, transform.position, Quaternion.identity);
             Object.Destroy(this.gameObject);
         }
     }      
 }

That's my clicking detection. While it does detect clicks, as I said before, it detects them for ALL tiles and activates them all. I only want it to activate on one of them. I've seen RayCasting stuff but I really need more explanation on how that thing works (if that's the solution).

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by nedas60 · Jun 26, 2017 at 01:48 PM

Aight, found the problem myself. Turns out that I could have ignored RayCasting. The only reason OnMouseDown function didn't work is because my camera was too close to the object. I moved my camera away by JUST 0.01 and it worked just fine. Moved it back and sure enough, it didn't work anymore. Lesson learned, don't have your camera too close to the object.

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
0

Answer by ShadyProductions · Jun 26, 2017 at 10:55 AM

Input.GetMouseButtonDown is triggered everytime you click your mousebutton.

You will have to retrieve your MousePosition and by the hand of the position you will have to figure out if you're in the bounds of your opentile's position to know if you're clicking the opentile. The easiest way to do this is use raycasting.

                 if (Input.GetMouseButtonDown(0))
                 {
                    RaycastHit hit; 
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
                    if ( Physics.Raycast (ray,out hit,100.0f)) {
                         if (hit.collider.gameObject.transform.position = opentile.transform.position)
                         {
                             Instantiate(openTile, transform.position, Quaternion.identity);
                             Object.Destroy(this.gameObject);
                         }
                     }
                 }
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 nedas60 · Jun 26, 2017 at 01:04 PM 0
Share

That gives me a Null Reference because openTile is what I want to spawn in, not what I have. I want to check if I click on this object, not the object I will instantiate AFTER a click. I tried setting it to this.gameObject but that still gives me an exception.

Follow this Question

Answers Answers and Comments

65 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

Related Questions

Detect OnMouseUp on gameobject with different colliders 1 Answer

Is there any way to check if two prefabs are touched at the same time 1 Answer

Detect mouse click even if unity game run in the background or if I click outside the unity game 0 Answers

Mouse over UI elements won't work 1 Answer

HELP C# script mouse position doesn't work 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