Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnDrag, DropZone, Add, Subtract.

Discussion in 'Scripting' started by Fressno, Oct 19, 2018.

  1. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Hey.
    Ive been going out of my mind trying to get this to work.
    Im trying to make the game "Towers of Hanoi" using Quill18´s drag and drop tutorial.

    Im in no need of placeholders to replace the discs while they are being dragged.

    My question is, how in the world do I substract the discvalues from when you start to drag a disc from peg 2?
    I can with the drop function add values, but i cant subtract them. It gives me an error:

    NullReferenceException: Object reference not set to an instance of an object
    Draggable.OnBeginDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/Draggable.cs:19)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IBeginDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:64)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IBeginDragHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()

    here is the scripts:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    8. {
    9.     public Transform parentToReturnTo = null;
    10.  
    11.     public int discNumber;
    12.     public Dopzone dopzone;
    13.  
    14.  
    15.     GameObject placeholder = null;
    16.  
    17.     public void OnBeginDrag(PointerEventData eventData)
    18.     {
    19.         dopzone.SubtractNumber();
    20.  
    21.        /* placeholder = new GameObject();
    22.         placeholder.transform.SetParent(this.transform.parent);
    23.         LayoutElement le = placeholder.AddComponent<LayoutElement>();
    24.         le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
    25.         le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
    26.         le.flexibleHeight = 0;
    27.         le.flexibleHeight = 0;*/
    28.  
    29.       //  placeholder.transform.SetSiblingIndex(this.transform.GetSiblingIndex());
    30.  
    31.  
    32.         parentToReturnTo = this.transform.parent;
    33.         this.transform.SetParent(this.transform.parent.parent);
    34.         GetComponent<CanvasGroup>().blocksRaycasts = false;
    35.     }
    36.  
    37.     public void OnDrag(PointerEventData eventData)
    38.     {
    39.         this.transform.position = eventData.position;
    40.     }
    41.     public void OnEndDrag(PointerEventData eventData)
    42.     {
    43.         this.transform.SetParent(parentToReturnTo);
    44.        // this.transform.SetSiblingIndex(placeholder.transform.GetSiblingIndex());
    45.  
    46.         GetComponent<CanvasGroup>().blocksRaycasts = true;
    47.         Destroy(placeholder);
    48.     }
    49.  
    50.  
    51.  
    52. }
    53.  
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections.Generic;
    5.  
    6. public class Dopzone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
    7. {
    8.     public int discSum;
    9.     public Text text;
    10.     public int pegNumber;
    11.     Draggable draggable;
    12.  
    13.     public static Stack<int> stackster = new Stack<int>();
    14.  
    15.     private void Awake()
    16.     {
    17.  
    18.     }
    19.  
    20.  
    21.     public void OnDrop(PointerEventData eventData)
    22.     {
    23.         //Debug.Log(eventData.pointerDrag.name + " was dropped on" + gameObject.name);
    24.         Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
    25.         if(d!= null)
    26.         {
    27.             d.parentToReturnTo = this.transform;
    28.             if (pegNumber==2)
    29.             {
    30.                 discSum = d.discNumber + discSum;
    31.                TextUpdater();
    32.             }
    33.             else
    34.             {
    35.                 return;
    36.             }
    37.  
    38.         }
    39.     }
    40.     public void SubtractNumber()
    41.     {
    42.         discSum = draggable.discNumber - discSum;
    43.         TextUpdater();
    44.     }
    45.  
    46.     public void TextUpdater()
    47.     {
    48.         text.text = "DiscSums " + discSum.ToString();
    49.     }
    50.  
    51.  
    52.     public void OnPointerEnter(PointerEventData eventData)
    53.     {
    54.  
    55.     }
    56.     public void OnPointerExit(PointerEventData eventData)
    57.     {
    58.  
    59.     }
    60. }
    Picture:
    http://www.gamedesignstefan.se/error/
     
  2. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    The error says it cant find a reference on line 19 of Draggable.cs. Assuming these line numbers above are exactly the same as in Unity, that would mean it can't find a reference to dopzone. Have you populated the reference in the inspector?
     
  3. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    Im still getting the error:
    NullReferenceException: Object reference not set to an instance of an object
    Draggable.OnBeginDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/Draggable.cs:19)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IBeginDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:64)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IBeginDragHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()

    I have populated in the inspector.
    Should i make a empty gameobject that holds the script dopzone on it, and make every peg referenced as a gameobject instead? so on "manager" keeps track of what is what?
    now the middle red peg is what acts like a manager.

    thanx for the help anyway.
     
  4. Fressno

    Fressno

    Joined:
    Mar 31, 2015
    Posts:
    185
    I used this:
    Code (CSharp):
    1.   public void Start()
    2.     {
    3.         dopzone = (Dopzone)FindObjectOfType(typeof(Dopzone));
    4.     }
    to force it to find the script "Dopzone".
    and it worked.