SpriteRenderer enables by itself when server starts

The SpriteRenderer always gets enabled whenever the gameObject becomes active (e.g. spawned) and I can’t figure out why. This is not related to the selection, no selection is happening, it is just the sprite renderer. I have even tried adding another child gameObject to the prefab with a SpriteRenderer and it also gets enabled along with the gameObject. The same effect is produced by any NetworkIdentity with a SpriteRenderer. Other disabled components stay disabled.

Before starting the server (activating the object):

After pressing Host button and thus activating the object:

What can I do to prevent the SpriteRenderer from enabling on itself? So far I have solved this by disabling the whole gameObject instead, but I am not sure if that is the correct approach.

(I am using Unity 2020.3)

1 Like

Hi there,
This bug has been cropping up a lot this week, I think it’s related to the latest version of Mirror. What version of Mirror are you using? I think it’s occurring between the start() and update method of the unit, so you can just disable it in the first update() call and it should be fine.

Oh, yes. Thanks for the answer. I am using Mirror 32.1.4. I have found this bit of code inside NetworkIdentity.cs which explains why this happens:

internal void OnSetHostVisibility(bool visible)
        {
            foreach (Renderer rend in GetComponentsInChildren<Renderer>())
                rend.enabled = visible;
        }

This is called with argument true inside OnHostClientSpawn().

EDIT: Apparentely, the proper solution is to change the NetworkIdentity’s visibility to ‘ForceHidden’. I have tested it and it indeed solves this issue. Mirror does not really document well what the ForceHidden option does. One would expect it would disable all renderers, but it instead just ignores them.

1 Like

Hello there! I got the same issue. Every unit spawned on the host appeared with the sprite enabled. This is not the case for any other client.

Unity version 2020.3.0f1, Mirror version : 32.1.4

Here is how I handled this. Instead of disabling the SpriteRenderer, I disabled the child GameObject itself:

Then I changed the event handlers to call the SetActive() method of this same child GameObject :
image

It works fine! :wink:

EDIT : Before setting this up, I tried to set the NetworkIdentity.Visible attribute to Force Hidden on the Unit prefab, as mentioned above, but I got an error when right clicking to move the unit after it being spawned…

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

Privacy & Terms