using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
using System.Collections.Generic;
public class DragSelection : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public List<GameObject> selected = new List<GameObject>();
public List<GameObject> selectable = new List<GameObject>();
[SerializeField]
Image selectionBoxImage;
Vector2 startPostion;
Rect selectionRect;
public void OnBeginDrag(PointerEventData eventData)
{
if (Input.GetMouseButton(0))
{
selectionBoxImage.gameObject.SetActive(true);
startPostion = eventData.position;
selectionRect = new Rect();
}
}
public void OnDrag(PointerEventData eventData)
{
if (Input.GetMouseButton(0))
{
//X axis
if (eventData.position.x < startPostion.x)
{
selectionRect.xMin = eventData.position.x;
selectionRect.xMax = startPostion.x;
}
else
{
selectionRect.xMin = startPostion.x;
selectionRect.xMax = eventData.position.x;
}
//Y axis
if (eventData.position.y < startPostion.y)
{
selectionRect.yMin = eventData.position.y;
selectionRect.yMax = startPostion.y;
}
else
{
selectionRect.yMin = startPostion.y;
selectionRect.yMax = eventData.position.y;
}
selectionBoxImage.rectTransform.offsetMin = selectionRect.min;
selectionBoxImage.rectTransform.offsetMax = selectionRect.max;
}
}
public void OnEndDrag(PointerEventData eventData)
{
selectionBoxImage.gameObject.SetActive(false);
foreach (GameObject selectObject in selectable)
{
if (selectObject != null)
{
Debug.Log("object found");
if (selectionRect.Contains(Camera.main.WorldToScreenPoint(selectObject.transform.position)))
{
selected.Add(selectObject);
Debug.Log("object selected");
}
}
}
}
}
im trying to make a box select feature in my game, and in the OnEndDrag method, I am trying to find all the selectable gameobjects in the box selector and add them to the selected list(Yes, I added gameobjects to the selectable list). the only error it gave me was:
NullReferenceException: Object
reference not set to an instance of an
object DragSelection.OnEndDrag
(UnityEngine.EventSystems.PointerEventData
eventData) (at Assets/AI
Project/DragSelection.cs:79)
UnityEngine.EventSystems.ExecuteEvents.Execute
(UnityEngine.EventSystems.IEndDragHandler
handler,
UnityEngine.EventSystems.BaseEventData
eventData) (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:78)
UnityEngine.EventSystems.ExecuteEvents.Execute[T]
(UnityEngine.GameObject target,
UnityEngine.EventSystems.BaseEventData
eventData,
UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1]
functor) (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
LINE 79: if (selectionRect.Contains(Camera.main.WorldToScreenPoint(selectObject.transform.position)));
This got me stumped because I cant seem to figure out what object reference isn’t set to an instance of an object. Usually I never have problems figuring out stuff like this but this time isn’t the case.
NOTE: this is my first time using the IBeginDragHandler, IDragHandler, and IEndDragHandler interfaces in a script, so I could have a lot wrong.