Ok, I have come to the point for the tutorial, where you are Testing the Game and Build. The Problem is that the game in the Editor is not even working, it throws up the Null Reference Error with the Unit Handler as Dino said it would do and stops that game completely. I can’t even test it. Anyone know how to get Unity to at least allow me to test and run though having the Null Reference. Both at around 12:30 and at 20:33 am I having this Issue. Sadly This is getting in the way of me completing the lesson and moving on. Here is my problem in Action:
Here is the Code, line 24 is what is is stopping the whole editor testing over:
using Mirror;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class UnitSelectionHandler : MonoBehaviour
{
[SerializeField] private RectTransform unitSelectionArea = null;
[SerializeField] private LayerMask layerMask = new LayerMask();
private Vector2 startPosition;
private RTSPlayer player;
private Camera mainCamera;
public List<Unit> SelectedUnits { get; } = new List<Unit>();
private void Start()
{
mainCamera = Camera.main;
*player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();*
}
private void Update()
{
if (player = null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (Mouse.current.leftButton.wasPressedThisFrame)
{
StartSelectionArea();
}
else if (Mouse.current.leftButton.wasReleasedThisFrame)
{
ClearSelectionArea();
}
else if (Mouse.current.leftButton.isPressed)
{
UpdateSelectionArea();
}
}
private void StartSelectionArea()
{
foreach (Unit selectedUnit in SelectedUnits)
{
selectedUnit.Deselect();
}
SelectedUnits.Clear();
unitSelectionArea.gameObject.SetActive(true);
startPosition = Mouse.current.position.ReadValue();
UpdateSelectionArea();
}
private void UpdateSelectionArea()
{
Vector2 mousePosition = Mouse.current.position.ReadValue();
float areaWidth = mousePosition.x - startPosition.x;
float areaHeight = mousePosition.y - startPosition.y;
unitSelectionArea.sizeDelta = new Vector2(Mathf.Abs(areaWidth), Mathf.Abs(areaHeight));
unitSelectionArea.anchoredPosition = startPosition + new Vector2(areaWidth / 2, areaHeight / 2);
}
private void ClearSelectionArea()
{
unitSelectionArea.gameObject.SetActive(false);
if (unitSelectionArea.sizeDelta.magnitude == 0)
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask)) { return; }
if (!hit.collider.TryGetComponent<Unit>(out Unit unit)) { return; }
if (!unit.hasAuthority) { return; }
SelectedUnits.Add(unit);
foreach (Unit selectedUnit in SelectedUnits)
{
selectedUnit.Select();
}
return;
}
Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);
foreach (Unit unit in player.GetMyUnits())
{
Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);
if (screenPosition.x > min.x &&
screenPosition.x < max.x &&
screenPosition.y > min.y &&
screenPosition.y < max.y)
{
SelectedUnits.Add(unit);
unit.Select();
}
}
}
}
https://www.udemy.com/course/unity-multiplayer/learn/lecture/22899452#questions/17569364