RTS Course on Udemy Problem - Null Reference Stopping Game From Running

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

Hi there,

There is a common bug that when trying to get hold of the player object, the NetworkClient.connection has not been initiated yet.

So instead of returning the player or a null player object, it throws an error for the connection being null, which breaks the game.

The solution many students have used is to move the line where they get hold of a player into a coroutine with a 0.5 second delay. This gives the connection enough time to establish and avoids the error.

This is New to me is there a example of this begin used for a timer. Nor sure how to approach it for when I try to set up the name for it, it keeps throwing up a coding error in Visual Studio Over it: (CS0161 ‘UnitSelectionHandler.CountDown()’: not all code paths return a value Assembly-CSharp G:\GamesFiles-------\Desktop\DS Projects----Learning Unity\LWU-MultiRTS\Assets\RTS\Scripts\Core\UnitSystem\UnitSelectionHandler.cs 29 Active
):

private void Start()
{
    mainCamera = Camera.main;
    StartCoroutine(MyConroutine());
}

private IEnumerator CountDown() 
{
    float timer = 5f;

    if (player == null)
    {
        player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
    }
}

UPDATE!!! ((1 hour later) :

Got the code to work, and now the game runs, but for some reason I can only spawn a One Unit at a time only after so long it can be between 5 to 21 second each time. Did I do something wrong? Not sure but I will let everyone know if it becomes a bigger issue.:

IEnumerator CountDown() 
    {

        if (player == null)
        {
            yield return new WaitForSeconds(3);
            player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
        }
    }


    private void Update()
    {
        StartCoroutine(CountDown());

        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            StartSelectionArea();
        }
        else if (Mouse.current.leftButton.wasReleasedThisFrame)
        {
            ClearSelectionArea();
        }
        else if (Mouse.current.leftButton.isPressed) 
        {
            UpdateSelectionArea();
        }
    }

You don’t need a full 3 seconds in your countdown there. Try changing that to 0.5f or something maybe?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms