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 /
  • Help Room /
avatar image
1
Question by RicardoF1994 · Feb 04, 2018 at 12:20 PM · gameobjectdraggingonmouseoveronmousedrag

Unity2D: OnMouseDrag, OnMouseOver etc. out of a sudden not working for specific GameObject

Hi there,

I am working on a card game. I have a few cards and a few card placeholders. I have created a CardScript to add to each card in the game. I made it so I can drag the card and when it collides with the "PlaceHolder" then it gets pinned at that position as soon as you release the mouse button. This works fine. I am dragging around the cards to placeholders and it looks cool. But then, out of a sudden one or two of the cards do not react at all to my clicking and dragging. I tried logging something in the OnMouseDrag and OnMouseOver event methods, but they do not get fired at all! It's like suddenly the card does not exist anymore for MonoBehaviour. When I drag it around in the scene then suddenly it works again. Here is my CardScript, does someone has an explanation? Thanks so, so much! :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CardScript : MonoBehaviour {
 
     public bool MovementAllowed = true;
     private Vector3 originalPosition;
     private Vector3 placedPosition;
     private Vector3 lastPosition;
     private bool turnedAround = true;
     public Text CardText;
     private Sprite originalSprite;
     private bool isMoving = false;
     public bool mayFlip = true;
 
 
     // Use this for initialization
     void Start () {
         originalPosition = transform.position;
         placedPosition = originalPosition;
         lastPosition = placedPosition;
         originalSprite = gameObject.GetComponent<SpriteRenderer>().sprite;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "PlaceHolder")
         {
             ChangePlacedPosition(collision.gameObject.transform.position);
         }
     }
 
     private void OnMouseOver()
     {
         if (mayFlip)
         {
             Debug.Log("Mouse over " + gameObject.name);
             if (Input.GetMouseButtonDown(1))
             {
                 FlipCard();
             }
         }
     }
 
     private void OnMouseDrag()
     {
         if (MovementAllowed)
         {
             if (!isMoving)
             {
                 Debug.Log("Trying to move " + gameObject.name);
             }
             isMoving = true;
             CameraDragScript.mayDrag = false;
             Vector3 followPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             transform.position = new Vector3(followPos.x, followPos.y, 0);
         }
     }
 
     private void OnMouseUp()
     {
         isMoving = false;
         CameraDragScript.mayDrag = true;
         transform.position = placedPosition;
     }
 
     public void ChangePlacedPosition(Vector3 position) //Change the position that the card will have when the mouse is released after dragging it around
     {
         lastPosition = placedPosition;
         placedPosition = position;
     }
 
     public void FlipCard()
     {
         if (turnedAround)
         {
             turnedAround = false;
             //Do flip animation, rotate y to -89
             gameObject.GetComponent<SpriteRenderer>().sprite = DataKeeperScript.Instance.EmptyCardSprite;
             PlaceTextOnCard();
             //Do flipover animation, rotate y from -89 back to 0
         }
         else
         {
             turnedAround = true;
             if (CardText != null)
             {
                 Destroy(CardText.gameObject);
             }
             gameObject.GetComponent<SpriteRenderer>().sprite = originalSprite;
         }
         
     }
 
     public void PlaceTextOnCard()
     {
         CardText = Instantiate(UIHandlerScript.Instance.text, GameObject.Find("Canvas").transform);
         CardText.rectTransform.position = Camera.main.WorldToScreenPoint(transform.position);
     }
 }
 
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

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

Related Questions

OnMouseOver wont work with fps Controller 0 Answers

Dragging on First clicking? 1 Answer

I want to put a limit on the edges of the screen for dragable objects 1 Answer

How to click and drag a game object along a mesh 0 Answers

Very weird. Transform position is at two places at the same time. 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