Previously, I apologize for my bad English. I am making a unit selection, and when the unit is selected, an error like this will appear:
NullReferenceException: Object reference not set to an instance of an object
PlayerManager.SelectUnit (UnityEngine.Transform unit, System.Boolean isMultiSelect) (at Assets/Scripts/PlayerManager.cs:71)
PlayerManager.Update () (at Assets/Scripts/PlayerManager.cs:56)
NullReferenceException: Object reference not set to an instance of an object
PlayerManager.DeselectUnits () (at Assets/Scripts/PlayerManager.cs:78)
PlayerManager.Update () (at Assets/Scripts/PlayerManager.cs:50)
this is the code for PlayerManager.cs
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using UnityEngine;
using UnityEngine.UI;
public class PlayerManager : MonoBehaviour
{
List<Transform> selectedUnits = new List<Transform>();
Vector3 selectMousePos;
bool isDragging = false;
private void OnGUI()
{
if (isDragging)
{
var rect = ScreenHelper.GetScreenRect(selectMousePos, Input.mousePosition);
ScreenHelper.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
ScreenHelper.DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
selectMousePos = Input.mousePosition;
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null)
{
// Debug.Log(hit.transform.tag);
if (hit.transform.CompareTag("Player"))
{
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
}
else
{
isDragging = true;
// DeselectUnits();
}
}
}
if (Input.GetMouseButtonUp(0))
{
if (isDragging)
{
DeselectUnits();
foreach (var selectableObject in FindObjectsOfType<BoxCollider2D>())
{
if (IsWithinSelectionBounds(selectableObject.transform))
{
SelectUnit(selectableObject.transform, true);
}
}
isDragging = false;
}
}
}
private void SelectUnit(Transform unit, bool isMultiSelect = false)
{
if (!isMultiSelect)
{
DeselectUnits();
}
selectedUnits.Add(unit);
unit.Find("Highlight").gameObject.SetActive(true);
}
private void DeselectUnits()
{
for (int i = 0; i < selectedUnits.Count; i++)
{
selectedUnits[i].Find("Highlight").gameObject.SetActive(false);
}
selectedUnits.Clear();
}
private bool IsWithinSelectionBounds(Transform transform)
{
if (!isDragging)
{
return false;
}
var cam = Camera.main;
var viewportBounds = ScreenHelper.GetViewportBounds(cam, selectMousePos, Input.mousePosition);
return viewportBounds.Contains(cam.WorldToViewportPoint(transform.position));
}
}
this is the code for ScreenHelper.cs
using UnityEngine;
public static class ScreenHelper
{
static Texture2D _whiteTexture;
public static Texture2D WhiteTexture
{
get
{
if (_whiteTexture == null)
{
_whiteTexture = new Texture2D(1, 1);
_whiteTexture.SetPixel(0, 0, Color.white);
_whiteTexture.Apply();
}
return _whiteTexture;
}
}
public static void DrawScreenRect(Rect rect, Color color)
{
GUI.color = color;
GUI.DrawTexture(rect, WhiteTexture);
}
public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
{
//Top
DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
// Left
DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
// Right
DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
// Bottom
DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
}
public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
{
// Move origin from bottom left to top left
screenPosition1.y = Screen.height - screenPosition1.y;
screenPosition2.y = Screen.height - screenPosition2.y;
// Calculate corners
var topLeft = Vector3.Min(screenPosition1, screenPosition2);
var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
// Create Rect
return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
public static Bounds GetViewportBounds(Camera camera, Vector3 screenPosition1, Vector3 screenPosition2)
{
var v1 = camera.ScreenToViewportPoint(screenPosition1);
var v2 = camera.ScreenToViewportPoint(screenPosition2);
var min = Vector3.Min(v1, v2);
var max = Vector3.Max(v1, v2);
min.z = camera.nearClipPlane;
max.z = camera.farClipPlane;
var bounds = new Bounds();
bounds.SetMinMax(min, max);
return bounds;
}
}