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
0
Question by zeroaleph · Jun 11, 2014 at 11:17 AM · collidersceneloadlevelonmousedownclickable

BoxCollider 2D not working OnMouseDown()

Hello everyone. I am kinda new to Unity, and I have a question: I can't seem to get to work a BoxCollider2D with an OnMouseDown() script attached. I have two different scenes. In the first scene, this works just fine. I set a BoxCollider2D on a image, so that when it gets clicked it changes to a new scene. While it works on scene 1, it doesn't work on scene 2. I just can't understand what is wrong. Apparently there is a problem whenever I set the box collider center x > 2. Anyway, here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class ChangeEnvironment : MonoBehaviour {
     
     public int level;
 
     void OnMouseDown(){
 
         Debug.Log("Down");
         Application.LoadLevel(level);
 
     }
 
 }

And here's a screenshot of the level: alt text

screen shot 2014-06-11 at 13.00.43.png (486.8 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 kweiko · Jun 14, 2014 at 07:02 PM 0
Share

Hi zeroaleph,

Are you increasing "level"? How does the image transfer to the next scene? Or if you are using two different instances of the image: are you including the script and the collider on both images? Because it's working on the first scene but not the other, compare the attributes of both images and see what differs.

As for the collider, not sure how it works, but the problem could be that you're moving only the collider and not the entire object, which will place the image and the collider on different places.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by YoungDeveloper · Jun 11, 2014 at 11:19 AM

Hi, i suggest casting a ray from your touch/mouse click point forward, if it hits collider, get the data you need from it, and from this point you do whatever you want.

Comment
Add comment · Show 6 · 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 zeroaleph · Jun 11, 2014 at 11:46 AM 0
Share

Hey, thanks! I can't seem to get this to work either. But that's because of my lack of dev skills I guess. I would have to cast a ray from Input.mousePosition, but in which direction? It's a 2D game... I want to be able to have more than one collider per environment, so if I have to tag every single collider individually to get them it becomes overly complicated. Can I ask you for the code of what you proposed?

avatar image Omer.Hussain · Jun 11, 2014 at 11:53 AM 0
Share

uncheck is trigger then try ... :/

avatar image zeroaleph · Jun 11, 2014 at 12:03 PM 0
Share

unchecking is trigger doesn't make any difference, and I don't see why it should have anyway :)

avatar image YoungDeveloper · Jun 11, 2014 at 03:53 PM 0
Share
 if(Input.GetButtonDown("Fire1")){
     RaycastHit hit;
     Ray ray =  Camera.main.ScreenPointToRay(Input.mousePosition);
     Debug.Log("Click");
 
     if(Physics.Raycast(ray, hit)){
         Debug.Log("Ray hit something");
     }
 }

You dont need to use tag for every item, create seperate script which will hold the index number of what type of item is that.

avatar image zeroaleph · Jun 11, 2014 at 05:22 PM 0
Share

Thanks a lot, but I don't know why, it doesn't work. It detects the click, but it does not hit the collider even if I click on it. (Oh, btw I changed the Physics.Raycast(ray,hit) to Physics.Raycast(ray, out hit)) :( I just don't understand why...

Show more comments

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

23 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

Related Questions

Clickable area problem? 0 Answers

How do I make a scene change when my Player collides with a item? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

OnMouseDown() not called on child object collider 2 Answers

Load / Stream 3rd Party Scene? 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