Hi, I’m a beginner to C# code and having trouble figuring out how to get a certain game feature to work.
I want the player to be able to drag images into slots, press a button to check, and receive feedback depending on if they are correct or incorrect. I got the drag and drop system working correctly, but no matter what slot they drag the image into, it reads as correct.
I tried putting the same tag on the correct slot and image to tell if it’s correct, as seen below:
if (gameObject.tag == otherObject.tag) {
correctLetter = true;
Debug.Log("Correct Letter");
}
else{
correctLetter = false;
}
but no matter the input, it returns as true every time. How do I fix this?
Here’s my full code, if needed:
DragDrop.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragDrop : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IDropHandler
{
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
private void Awake(){
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("OnDrag");
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("OnPointerDown");
}
public void OnDrop(PointerEventData eventData)
{
throw new System.NotImplementedException();
}
}
ItemSlot.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemSlot : MonoBehaviour, IDropHandler
{
public bool correctLetter;
public GameObject otherObject;
public GameObject objectToDrag;
public GameObject objectDragTo;
public float dropDistance;
public void OnDrop(PointerEventData eventData) {
correctLetter = false;
Debug.Log("OnDrop");
if (eventData.pointerDrag != null) {
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
if (gameObject.tag == otherObject.tag) {
correctLetter = true;
Debug.Log("Correct Letter");
}
else{
correctLetter = false;
}
}