So, I started making a card game like Slay the Spire. I am pretty new to Unity, so I mostly do not know what I am doing. I made a card game object and an enemy. I also made the card draggable with mouse in game. I gave the enemy hp with using text mesh and I typed there something like ----. What I wanted them to do is that when I drag the card to the enemy one of the - would be reduced and the card will be destroyed. Instead, what they do is throwing me back to the scene (but the game still runs) and making me unable to move the card.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cardScript : MonoBehaviour
{
bool canMove;
bool dragging;
Collider2D collider;
private void Start()
{
collider = GetComponent<Collider2D>();
canMove = false;
dragging = false;
}
private void Update()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (collider == Physics2D.OverlapPoint(mousePos))
{
canMove = true;
}
else
{
canMove= false;
}
if (canMove)
{
dragging = true;
}
}
if (dragging)
{
this.transform.position = mousePos;
}
if (Input.GetMouseButtonUp(0))
{
canMove = false;
dragging = false;
}
}
private void OnTriggerEnter2D(Collider2D co)
{
if (co.name == "enemy")
{
co.GetComponentInChildren<Health>().decrease();
Destroy(gameObject);
}
}
}
this is the code for the card
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
TextMesh tm;
void Start()
{
tm = GetComponent<TextMesh>();
}
public int current()
{
return tm.text.Length;
}
public void decrease()
{
if (current() > 1)
tm.text = tm.text.Remove(tm.text.Length - 1);
else
Destroy(transform.parent.gameObject);
}
}
And this is the code for enemy hp.
Help