Netcode Object Visibility NotServerException on Server

Hey,

im currently trying to implement the object visibility feature of netcode in my game. It works fine but i get a weird error.

When i show objects with
no.NetworkShow(clientID)
that are not related to an actual player(NPC) it works fine but when i try to show a player object that corresponds to an actual client to another client i get a NoServerException which is weird because the code runs on the server.

To test it i added some logs which show me this:

Trying to show entity with client ID 2 to client with ID 1 (custom message that tells which client should be shown to which other client)

Server stats: IsServerOwner: False, IsServer: True, ServerIsHost: False (current stats of NetworkManager.Singleton)

Error Message: Only server can change visibility (Error message output of try catch block)

It states that a visbility change can only be done by the server which the code is running on.

I don’t assign a default player object to clients but rather call
networkObject.SpawnAsPlayerObject(clientID, true);

I tried calling
networkObject.RemoveOwnership();
after the spawn method so that the server is the owner. When i do that IsServerOwner output from above gets switched to true but the error still exists.

I don’t know if im missing something.

Im using:

Unity 6000.0.23f1
NGO: 2.0.0
Transport: 2.3.0

Thank you for your help. If you need more information please let me know.

1 Like

Have you found a solution?

I also seem to be having this problem when trying to update the visibility of player.

I even added a conditional to only do it on the server and I still get the following error.

NotServerException: Only server can change visibility

var networkObject = Instantiate(
    prefabToSpawn
).GetComponent<NetworkObject>();

if (IsServer)
{
    networkObject.SpawnAsPlayerObject(clientId);
    networkObject.NetworkShow(clientId);
}

Hey,

sadly i haven’t so far.
But at least i know now that im not the only one. Now it seems like a bug to me and not just an error on my side.

With that information i’ll investigate further and have a look at the boss room example.
If i cant reproduce it there ill create a minimal example and file a bug report

There was a Player related fix that went in not long ago, it’s not directly related to this but it would be worth updating to the latest version of NGO (2.2.0) anyway to see if this is still an issue.

Thats what i thought.
I tried to reproduce it right now in my game, but the bug did not appear anymore.
Im currently using netcode version 2.1.1.
I couldnt really find anything regarding that in the patch notes.

I reverted my project back to netcode version 2.0.0 and the bug appeared again. So it was indeed fixed in 2.1.1

Thanks @cerestorm to point that out

@vinleclair try to update to 2.1.1 and test it again

Looking at the code it’s also throwing the wrong exception as it should state the object is already visible:

      if (Observers.Contains(clientId))
            {
                if (NetworkManager.DistributedAuthorityMode)
                {
                    Debug.LogError($"The object {name} is already visible to Client-{clientId}!");
                    return;
                }
                else
                {
                    throw new NotServerException("Only server can change visibility");
                }
            }

Ok at least now i know what the problem was. Thanks for investigating

I am already using NGO 2.2.0 and I get the exception. I tried deleting my Unity package cache folder, deleting my Library folder and reverting to NGO 2.1.1 and I still get the error. So I think the bug is still present in the NGO package.

That being said thanks to @cerestorm I understood the issue. The exception is wrong but the problem is that the networkObject is already visible.

For example, if I update my code snippet to the following, I no longer get the exception.

networkObject.SpawnAsPlayerObject(clientId);
networkObject.NetworkHide(clientId);
networkObject.NetworkShow(clientId);

I am just starting to learn NGO so that explains my confusion about not knowing that the object was already visible.

If you want to control the visibility of an object with NetworkShow/Hide you can have it initially hidden if you untick Spawn With Observers on the NetworkObject component (actually this used to be required, I’m not sure if it still is).

How the host handles objects set as hidden from itself I don’t know, I’ve only ever used visibility when running server only.

1 Like

Alright thanks for the heads up! I still have a lot to learn about Unity and NGO in particular.

Something that came back to me, you can use networkObject.IsNetworkVisibleTo(clientId) to check whether an object is already hidden/shown.

1 Like