• 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 · gameobjectsetactiveonmousedownboxcolliderclick 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

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

Script is losing references once gameobjects get disabled. How can I fix this? 0 Answers

Two script using the same box Collider 0 Answers

Calling setActive(false) on a disabled object? 3 Answers

game object set active on trigger enter? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges