• 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 golivrob · Sep 27, 2017 at 10:22 PM · scriptingbasicsonmousedown

OnMouseDown, where is my error?

Hello,

I'm trying to build something like a puzzle. Images are already at the correct spot but not with the correct rotation. The player has to click on the image to turn them. I guess I'm making an error because this is not working. I find some topics such as http://answers.unity3d.com/questions/1397225/onmousedown-on-another-object-1.html dealing about that but I haven't managed to reproduce it.

Here is the code I have so far:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Rotate : MonoBehaviour {

 public GameObject Object_A_Tourner;

 void OnMouseDown()
 {

     transform.Rotate(new Vector3(0f, 0f, 90f));

 }

}

This code is apply to my 25 images, at this time nothing is moving when I click on one of them. Have you any though?

Cheers, Mathieu

Comment
Add comment · Show 2
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 dvidunis · Sep 27, 2017 at 10:29 PM 0
Share

Did you add a collider? Try setting the "Trigger" on or off.

avatar image golivrob dvidunis · Sep 27, 2017 at 10:42 PM 0
Share

Thanks for the quick reply. Isn't it supposed to be for a collision between two objects?

At the very first step I was using (without success) this ins$$anonymous$$d of On$$anonymous$$ouseDown:

void Update() { if (Input.Get$$anonymous$$ouseButtonDown(0)) { Object_A_Tourner.transform.Rotate(0, 0, 90); } }

I'll take a closer look to the collider tomorrow to know more about how it works.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Detour_Guide · Sep 28, 2017 at 05:22 AM

As @dvidunis said, you need a collider on these objects. OnMouseDown only gets called if an object with a collider attached (or a GUIElement) is clicked on.

The reason that this is the case, is because a RayCast is performed and if that RayCast intersects a Collider, it will fire off the OnMouseDown method.

OnMouseDown

RayCast

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 golivrob · Sep 28, 2017 at 02:02 PM 0
Share

I'll try it.

When you say this " (or a GUIElement) is clicked on", so I also can try to create buttons on each image with a script which make the image turns?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown on another object 1 Answer

Not getting debug.log errors from mysql? 0 Answers

hi guys no idea why I'm getting error CS0117: `UnityEngine.MasterServer' does not contain a definition for `ipAdress' 1 Answer

Using video Texture example (Texture not assigned ) 0 Answers

Duplicating GameObject with variations of the same functionality. 0 Answers


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