Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null reference for : SceneManager.CheckForAndSendNetworkObjectSceneChanged(); in NetworkManager Class #3208

Open
amirhoseynfatemi opened this issue Jan 16, 2025 · 3 comments
Assignees
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. stat:Investigating Issue is currently being investigated type:bug Bug Report

Comments

@amirhoseynfatemi
Copy link

### Description

I made an online game with NGO and it shows me a lot of null reference errors.

And this error only happens in Android and is not a problem in Unity!

With Android Logcat, I found out that the error is in the Unity NetworkManager script and from this part(Screenshots).

In Android, when the player enters the scene where the NetworkObject is located
In the scene, I have a script to connect to the server, this script works well in Unity
But such a problem occurs in Android
I have no idea where the problem is!!!

Can anyone help me, what is the cause of this error? Thank you.

Screenshots

Image

Image

Environment

  • OS: Android
  • Unity Version: 6.0.32f1
  • Netcode Version: 2.2.0
@amirhoseynfatemi amirhoseynfatemi added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report labels Jan 16, 2025
@NoelStephensUnity
Copy link
Collaborator

That error means that for some reason the NetworkManager is no longer valid.
Could you provide us with a description of what events occur up to getting this error (i.e. describe steps taken from starting a game session up to the point of the error happening)?

@NoelStephensUnity NoelStephensUnity added stat:awaiting response Status - Awaiting response from author. stat:Investigating Issue is currently being investigated labels Jan 17, 2025
@amirhoseynfatemi
Copy link
Author

amirhoseynfatemi commented Jan 17, 2025

Hi @NoelStephensUnity
Thanks for Reply

I have a scene that contains a NetworkManager as well as a GameObject with a script for connecting a client to a dedicated server.

In the Start method of this script, I subscribe to OnConnectionEvent and OnTransportFailure.
Then, I specify the IP and port using SetConnectionData, and I call StartClient in the next line.

In the NetworkManager, I use a prefab named HeroPresent as the Default Player Prefab. When this prefab is spawned, it triggers the OnNetworkSpawn method, where a ServerRpc informs the server which player should be spawned.
Calling the ServerRpc method can only be done by the Owner and not the Server, as shown below:

public override void OnNetworkSpawn()
{
    if (!IsOwner || IsServer) return;
    // .......
}

The server then instantiates the desired player, accesses its NetworkObject, and spawns it using SpawnAsPlayerObject.

Each time this process completes, the server checks how many players have spawned in the game. If the number of spawned players reaches the desired count, the server starts the game.
At this point, a ClientRpc notifies all connected players that the game has started.

The method OnConnectionEvent, which is subscribed to OnConnectionEvent, is implemented as follows:

private void OnConnectionEvent(NetworkManager arg1, ConnectionEventData arg2)
    {
        switch (arg2.EventType)
        {
            case ConnectionEvent.ClientConnected:
                waitPageText.text = "Waiting for an opponent to join!";
                break;

            case ConnectionEvent.PeerConnected:
                waitPage.gameObject.SetActive(false);
                MessageShower.Instance.Show("Your opponent has joined the game");
                break;
            case ConnectionEvent.ClientDisconnected:
                
                break;
            case ConnectionEvent.PeerDisconnected:
                    MessageShower.Instance.Show("Your opponent has left the game");
                break;
        }
    }

Additionally, there is a method named CancelGame, which is subscribed to OnTransportFailure. This method simply loads another scene in Single mode.

This is the entire process I follow. However, I encounter errors on Android, even though everything works properly in the Unity Editor.

@michalChrobot michalChrobot removed the stat:awaiting response Status - Awaiting response from author. label Jan 17, 2025
@NoelStephensUnity NoelStephensUnity self-assigned this Jan 17, 2025
@amirhoseynfatemi
Copy link
Author

@NoelStephensUnity

Hello again,

So, I found the problem.
The issue was with how the scene was being loaded. I was loading the scenes I had using Addressable. When I loaded them using SceneManager, everything worked correctly on Android too. However, I couldn't understand why this problem occurs when using Addressable. Should I clear the Addressable cache before loading the scene?

- The code for loading the scene using Addressable:

var asyncOperation = Addressables.LoadSceneAsync(sceneName, LoadSceneMode.Single);

            // Wait until the scene is loaded
            yield return asyncOperation;

            if (asyncOperation.Status != AsyncOperationStatus.Succeeded)
            {
                Debug.LogError("Failed to load scene");
                yield break;
            }

- The code for loading the scene using SceneManager:

var asyncOperation = SceneManager.LoadSceneAsync(scene.ToString());

        while (asyncOperation is { isDone: false })
            yield return null;


        if (asyncOperation is not { isDone: true }) yield break;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. stat:Investigating Issue is currently being investigated type:bug Bug Report
Projects
None yet
Development

No branches or pull requests

3 participants