How to compare name of a GameObject in Unity

So I'm making simple game to put the right answer in the right slot. which is when you put the right sticker on the slot you got score. for the example if you put the sticker "a" to the slot "b" then you got point. and then the sticker "a", the sticker become child of slot "b". the problem is when i uses the name of the gameobject it doesnt work, i tried using debug.log to show the score but it doesnt work. this script is component of the slot. the draghandler is from another script the script is component of the sticker. Here is the code

public void OnDrop (PointerEventData eventData)
{
    if (!item) {
        DragHandler.itemBeingDragged.transform.SetParent (transform);
        if (DragHandler.itemBeingDragged.gameObject.name == "b" && DragHandler.itemBeingDragged.transform.parent.name == "slot2") {
            score = score + 25;
            nilai = score.ToString();
            Debug.Log ("score: "+nilai);
        }}}

but when i used this code to show the name of slot and the sticker it's work

public void OnDrop (PointerEventData eventData)
{
    if (!item) {
        DragHandler.itemBeingDragged.transform.SetParent (transform);
        Debug.Log ("slot: "+DragHandler.itemBeingDragged.transform.parent.name + "item : "+DragHandler.itemBeingDragged.gameObject.name);
        }
    }

this is the code for draghandler

using UnityEngine;

using System.Collections; using UnityEngine.EventSystems;

public class DragHandler : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler {

public static GameObject itemBeingDragged;
Vector3 startPosition;
Transform startParent;

#region IBeginDragHandler implementation

public void OnBeginDrag (PointerEventData eventData)
{
    itemBeingDragged = gameObject;
    startPosition = transform.position;
    startParent = transform.parent;
    GetComponent<CanvasGroup> ().blocksRaycasts = false;
}

#endregion

#region IDragHandler implementation
public void OnDrag (PointerEventData eventData)
{
    startPosition = Input.mousePosition;
    //Debug.Log ("namanya : " + itemBeingDragged.name);
}
#endregion

#region IEndDragHandler implementation

public void OnEndDrag (PointerEventData eventData)
{
    itemBeingDragged = null;
    GetComponent<CanvasGroup> ().blocksRaycasts = true;
    if (transform.parent == startParent) {
        transform.position = startPosition;
    }
}

#endregion

}

If there's something missing with my explanation let me know. Thank you.

Upvotes: 0

Views: 1237

Answers (1)

user6662323
user6662323

Reputation:

I can't see the code for the drag handler but does it drop the item after you release it? If so it may not have the item that WAS being dragged. So maybe when you check the DragHandler.itemBeingDragged it may return null.

Upvotes: 0

Related Questions