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 hollym16 · Nov 27, 2013 at 03:55 PM · gameobjectsetactiveboxcollideronmousedownclick and drag

Conflicting Box Colliders

I have a 2D menu that has a box collider on it to allow it to scroll up and down. I then have Empty GameObjects with box colliders over the top of where I want the buttons to be (to select a level). However, when both sets of box colliders are on, if I try and click and drag the menu, it selects the Empty GameObject level button and takes me to that level. I've tried putting in script to make the Empty GameObject button inactive OnMouseDown and OnMouseDrag but that does nothing. Here's the script I have so far:

 using UnityEngine;
  
 using System.Collections;
  
 [RequireComponent(typeof(BoxCollider))]
  
 public class Drag : MonoBehaviour
 {
     private Vector3 screenPoint;
     private Vector3 offset;
     private float _lockedYPosition; 
     public Vector2 limits = new Vector2(-20, 20);
     
     private GameObject Level1;
     private GameObject Level2;
     private GameObject Level3;
     private GameObject Level4;
     private GameObject Level5;
     
     void Start () {
         Level1 = GameObject.Find("Levels/Level1");
         Level2 = GameObject.Find("Levels/Level2");
         Level3 = GameObject.Find("Levels/Level3");
         Level4 = GameObject.Find("Levels/Level4");
         Level5 = GameObject.Find("Levels/Level5");
         
         Level1.SetActive(true);
         Level2.SetActive(true);
         Level3.SetActive(true);
         Level4.SetActive(true);
         Level5.SetActive(true);
     }
      
     void OnMouseDown() {
      
        _lockedYPosition = screenPoint.y;
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
            
         Level1.SetActive(false);
         Level2.SetActive(false);
         Level3.SetActive(false);
         Level4.SetActive(false);
         Level5.SetActive(false);
         
               
     }
  
     void OnMouseDrag() 
     { 
       
         
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     curPosition.x = _lockedYPosition;
     transform.position = curPosition;
    
     curPosition.y = Mathf.Clamp(curPosition.y, limits.x, limits.y);
 if(curPosition.y > limits.y){
     curPosition.y = limits.y;
 }
 else if(curPosition.x < limits.x){
     curPosition.x = limits.x;
             
         Level1.SetActive(false);
         Level2.SetActive(false);
         Level3.SetActive(false);
         Level4.SetActive(false);
         Level5.SetActive(false);
 }
     }
 }

I'm sure there must be a way of checking that if the mouse is dragging, the box colliders should not be active?

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

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

How to disable an object by clicking outside anywhere else? 1 Answer

Making A Debug.Log depending on what GameObject is active 1 Answer

Zombie won't die 2 Answers

collisions in character not working! 2 Answers

Calling setActive(false) on a disabled object? 3 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