Search Unity

Question Shader not working once multiple clients connect to a multiplayer room

Discussion in 'Multiplayer' started by NachoDevelopments, Aug 31, 2020.

  1. NachoDevelopments


    Aug 13, 2015
    Here is what is happening, I have a sprite renderer component on a prefab that has a material created from a custom shader, which you can see here:

    Code (Boo):
    1. Shader "Unlit/PixelOposite"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("MainTexture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    9.     SubShader
    10.     {
    11.         Tags { "Queue"="Transparent" }
    12.         Lighting Off Cull Off ZWrite Off Fog { Mode Off }
    14.         Pass
    15.         {
    16.             Color [_Color]
    17.             AlphaTest Greater 0.5
    18.             Blend SrcColor DstColor
    19.             BlendOp Sub
    20.             SetTexture [_MainTex] { combine previous, texture * primary }
    21.         }
    22.     }
    23. }

    Nothing fancy, just inverting colors based on the background.

    When I instantiate this prefab while connecting to a multiplayer room with just one client (therefor I am the host), I can instantiate this prefab without problems. But, the moment I join the room with another instance of the game, so a new client joins the room, the sprite stops being rendered (The GameObject is still there, since it is shown in the scene hierarchy, an all components look ok). If I connect at the exact same time that the sprite is being rendered (this prefab is something that is instantiated at will and destroyed a few seconds after, so I doesn't have to be there when other players connect) it will work fine every time I instantiate it. So:

    First client connects -> Instantiates GO -> Renders fine

    First client connects -> Instantiates GO -> Second client connects -> GO is destroyed -> Any of them Instantiates GO -> Renders fine

    First client connects -> Second client connects -> Any of them Instantiates GO -> Does not render correctly

    Some notes about the prefab that may help:
    • It has an animator
    • The object is not synced over the network because is just relevant for the user instantiating it, so I just call Instantiate(...), but no NetworkServer.Spawn(...)

    Now, after a lot of debugging and thinking it was some sort of weird sync error on my end (since I am not very experienced with unet) I acctually found the error was the material/shader, since I can change it to a Sprites/Default and it will render fine (but I need to have that shader material).

    Any idea why this happens?

    Unity version: 2019.4.8f1 LTS
    I am using the Lightweight RP

    If any additional info is required don't hesitate to ask! Thank you.

    Last edited: Aug 31, 2020