Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Make script run on the Server/Client only?

Discussion in 'Multiplayer' started by Cathair, Apr 18, 2022.

  1. Cathair

    Cathair

    Joined:
    Mar 23, 2022
    Posts:
    3
    I am trying to structure the code in a more manageable way. I got inspiration from the "Boss Room" sample.
    Currently I have the following code structure:

    - PlayerShared
    - PlayerClient
    - PlayerServer

    Now I am looking for a way to fool-proof the scripts by restricting them to be run only on server or only on client. The problem is that it is easy to forget to plug the if (IsServer) into the life cycle callbacks of the corresponding scripts, e.g. PlayerClient.Update() or PlayerServer.Update() thus producing strange and unexpected behaviour.

    I guess that even if it were possible, theoretically it could lead to other problems such as impossibility to call a ServerRpc from the client code.

    Perhaps an alternative could be something like method decorators, that would restrict the whole method to the Client/Server e.g. [IsServer]?
     
  2. AggroBadger

    AggroBadger

    Joined:
    Feb 19, 2022
    Posts:
    16
    Perhaps you could throw an exception in the constructor if IsServer / IsClient isn't the desired value?