I am creating simple multiplayer game. I am familiar and know everything about sending messages client > server and server < client but now i came upon one question. How to check if object (in my case player character) is local or someone others?

I have one solution in head and it goes like this (i am developing in unity)

I have script which start at game start and connects to server. There it request connecting to server and handles if it gets rejected. Now what i thought is sending unique SESSION_ID when creating client on server and store it in public static int SESSION_ID inside that my script (let's call it Networking.cs). Then when creating Player character object i though about setting int Owner which will hold one of SESSION_ID. So then i simply create bool which compares session_id of global variable inside networking and one in object and see if object is local.

In code it would look something like this (without all code for server/client communication)

public class Networking
    public static int SESSION_ID;

    public Start()
        // here connect to server
        SESSION_ID = ReceivedSessionIDFromServer;

        // Creating ingame object
        GameObject p = Instantiate(PlayerCharacter, new Vector3(0, 0, 0), Quaternion.identity);

        // Adding Player Component
        p.GetComponent<NetworkObject>().ownerSessionID = SESSION_ID;

public class NetworkObject
    public int ownerSessionID;

    public Update()
              Debug.Log("Object is local!");

    bool IsLocal()
        if(this.ownerSessionID == Networking.SESSION_ID)
            return true;

        return false;

So is it good approach or i will have problem doing it this way?

  • 2
    \$\begingroup\$ Have you observed a problem doing it this way? If not, then it's probably fine for your context, and you don't need our permission to proceed. \$\endgroup\$ – DMGregory Jan 15 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.