Search Unity

What's the proper way of checking if code is running server side?

Discussion in 'Multiplayer' started by Yodzilla, Feb 20, 2018.

  1. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    I'm knee deep in converting Hastilude to a networked game and it's going fairly well but I'm completely befuddled by the correct way to see if code is running on the server vs the client. For example, if I instantiate an object with a NetworkIdentity and then do NetworkServer.Spawn on it, the following happens in the NetworkBehaviour when running on the server:
    Why is this? All three of those things (according to the docs and many examples across the internet) should be checking and returning the same thing but they're inconsistent which is extremely frustrating. What's the best and proper way to check server side code and what am I not understanding about the above?
     
  2. Ellernate

    Ellernate

    Joined:
    Aug 25, 2017
    Posts:
    81
    NetworkServer.active is a nice static method to call and from my experience is always correct
     
    ceitel likes this.
  3. Yodzilla

    Yodzilla

    Joined:
    Mar 21, 2013
    Posts:
    48
    But that's not what I'm looking for. I'm checking to see if the code is running on the server as opposed to a client, not if the server itself is active and running. As per the docs NetworkServer.active "checks if the server has been started", not WHERE a particular piece of code is being run.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If the server has been started then the code should be running on the server. Personally, in my game since I use server authority for every networked gameobject, I just check "if (hasAuthority)" which has never let me down.