0

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:

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);
    }
}

Does someone have an explanation? Thanks so, so much! :)

derHugo
  • 83,094
  • 9
  • 75
  • 115
Ricardo Frederiks
  • 127
  • 1
  • 3
  • 13
  • You should be using the `OnBeginDrag`, `OnDrag` and `OnEndDrag` function.... – Programmer Feb 04 '18 at 16:36
  • Could you explain to me why the methods I used work fine and then suddenly they don't for just one or two cards? What is going on behind the scenes that could make this happen? I'll try using the methods from the other question. – Ricardo Frederiks Feb 04 '18 at 17:25
  • I cannot explain why. By the way, you may want to put your `Debug.Log("Mouse over " + gameObject.name);` outside of the `if` statements. Maybe that's why you think they are not being called. – Programmer Feb 04 '18 at 17:29

0 Answers0