Null Ref - UnitSelectionHandler error

Hello,

I read through other threads about this same issue, and haven’t found the solution that works for me yet. Knowing me, it’s probably something simple I’ve overlooked.

I think it has something to do with there being no Player on the game, when it starts the player hasn’t been spawned and it crashes before unity spawns a player.

My code for my Unit Selection Handler is(Line 40):

The Video says not to worry about this and that you will still be able to build and test, I can do neither

I decided to continue working through the course, hoping that I could hold off until I get to the Lobby session to fix this issue, but with all the events being added, I’d really like to test my build at this point and see it all working.

Thanks in advance,

Hey there,
The instructors error is on GetComponent(), which sometimes returns null.
However, some students get an error on NetworkClient.connection, which breaks the script. The solution other students have used is to move that line into a coroutine and add a 0.5 second delay. This gives the connection time to set up before trying to use it to get the player.

1 Like

Since your reply I have tried using the Unity Docs to create a Coroutine, but can’t seem to find my way around it. I have no idea if I’m close or not, but currently I have:

IEnumerable NetworkClientWaitForSeconds()
{
player = NetworkClient.connection.identity.GetComponent();
yield return new WaitForSeconds(0.5f);
}

then under the Update()

if(player == null)
{
StartCoroutine (NetworkClientWaitForSeconds());
}

However the "NetworkClientWaitForSeconds() won’t be passed due to being a string. Am I close here, or on the wrong track?

*Edit: I just realised I’ve been putting it into the Update method, still trying in the meantime
**Edit Removed “StartCoroutine” /facepalm, code works for testing the game, but now throws the issue with foreach(Unit unit in player.GetMyUnits()). Seems to only crash when letting go of the selection field, even if no object are selected in the zone. But normal click + Shift click groupings work.

Few things:

Coroutines should return IEnumerator, not IEnumerable

Your code is the wrong way round. You need to wait first, then try the connection

IEnumerator NetworkClientWaitForSeconds()
{
    yield return new WaitForSeconds(0.5f);
    player = NetworkClient.connection.identity.GetComponent<Player>();
}

You should also not be calling it in Update(). This will start several coroutines before player is set.

Sorry, only saw these after I posted. You are still assuming that player is set and trying to call a function on it. When executing ClearSelectionArea (I see in your code this is the only place where you do the mentioned foreach) you should first check if player is null. I’m also assuming the error is still a null reference exception on player

1 Like

Hey, thanks for replying!
I have Updates the function to IEnumerator, and put under start:

if (player == null)
{
NetworkClientWaitForSeconds();
Debug.Log(“Started Player Wait”)
}

The console showed this function ran

I added

if(player == null)
{
Debug.Log(“Player Returned as null”)
foreach(Unit unit in player.GetMyUnits())
etc
}

The Console returns with my log and crashes when the selection field is let go
The Null REF error is:
image
Line 68 now being: ClearSelectionArea()
Line 137 now being: foreach(Unit unit in player.GetMyUnits())

Edit* spacing and spelling errors

This is not waiting because it’s no longer a coroutine

Is this the code? You are checking if player is null, logging it to the console if it is, and then also trying to access it at the same time. If player is null, you cannot call player.GetMyUnits()

1 Like

Oh my days,

I went through the documentation and overlooked how to use that method as a string.

Fixed by:

if (Player == null)
{
StartCoroutine(“NetworkClientWaitForSeconds”)
}

I removed it this morning becasue I was trying to use:
StartCoroutine (NetworkClientWaitForSeconds()) [which obviously didn’t work because it wasn’t a string]

thank you!!

You can use it as you originally had it:

if (Player == null)
{
    StartCoroutine(NetworkClientWaitForSeconds());
}

This is fine. The reason it didn’t work for you was because you had it as IEnumerable instead of IEnumerator.

However, you can also call it as a string. I would suggest you use nameof to prevent typos

if (Player == null)
{
    StartCoroutine(nameof(NetworkClientWaitForSeconds))
}
3 Likes

I just changed it to the method including nameof (thank you!)
I also tried using the method instead of the string and that also worked as you said

I know what I’ve done to bring in IEnumerable, I must have typed in the first few letters and not seen the difference. And I paid no attention to the documentation and my code of the method actually being different the whole time.

Thank you again Bixarrio

1 Like

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

Privacy & Terms