Game
The global game object provides access to core game state and information available on both client and server.
This is the base class for both client (GameClient) and server (GameServer) objects.
Use cases:
- Accessing all connected players in the world
- Getting world instance information (room ID, creator, privacy settings, etc.)
- Checking if running in Unity Editor
- Retrieving networked GameObjects by their network ID
Available on: Client and Server scripts
Properties
Provides information about the current world instance, including room settings, creator information, and privacy flags. See WorldInstanceInfo for all available properties (WorldId, RoomId, CreatorId, MaxPlayers, IsPrivate, etc.).
local worldInfo = game.info
print("Room ID: " .. worldInfo.roomId)
print("Creator: " .. worldInfo.creatorId)
print("Max Players: " .. worldInfo.maxPlayers)
if worldInfo.isPrivate then
print("This is a private room")
end
if worldInfo.allowsSpectating then
print("Spectating is allowed")
end
Indicates whether the script is currently running in the Unity Editor. Use this to enable editor-specific behavior, debugging features, or disable certain functionality during development.
-- Conditional behavior based on environment
if game.isEditor then
print("Running in Unity Editor - debug mode enabled")
-- Enable debug features
else
print("Running in production")
-- Disable debug features
end
-- Useful for development-only features
if game.isEditor then
-- Show detailed logs only in editor
print("Detailed debug information...")
end
The total number of players connected to the world instance (across all scenes).
Equivalent to #game.players.
IMPORTANT: In most cases, you should use scene.playerCount instead.
game.playerCountreturns total players across all scenesscene.playerCountreturns only players in a specific scene (recommended)
-- RECOMMENDED: Use scene.playerCount for current scene
print("Players in this scene: " .. scene.playerCount)
-- Use game.playerCount for total across all scenes
if game.playerCount >= game.info.maxPlayers then
print("Room is full!")
end
print("Total: " .. game.playerCount .. " = " .. #game.players)
An array of all the players currently connected to the world instance (across all scenes).
IMPORTANT: In most cases, you should use scene.players instead of game.players.
game.playersreturns ALL players in the entire world instancescene.playersreturns only players in a specific scene (recommended)
Use game.players only when you need to access players across all scenes.
-- RECOMMENDED: Use scene.players for players in current scene
for i, player in ipairs(scene.players) do
print(player.name .. " is in this scene")
end
-- Use game.players when you need all players across all scenes
print("Total players in world: " .. #game.players)
for i, player in ipairs(game.players) do
print(player.name .. " (ID: " .. player.user.id .. ")")
end
Methods
Retrieves a NetworkObjectValue for a GameObject by its network ID. Network IDs are used to identify networked GameObjects across clients and servers. Use this to access networked GameObjects when you have their network ID (e.g., from a NetworkObjectValue's value, or from another networked reference).
Returns a NetworkObjectValue that can be used to synchronize the GameObject reference across the network. Returns nil if the network ID doesn't exist or the object isn't found.
-- Get a network object by ID
local networkId = 12345
local networkObject = game.GetNetworkObject(networkId)
if networkObject ~= nil then
-- Access the actual GameObject
local gameObject = networkObject.value
if gameObject ~= nil then
print("Found object: " .. gameObject.name)
end
else
print("Network object not found")
end
-- Example: Store and retrieve a networked GameObject
local myObject = GameObject.Find("MyNetworkedObject")
local networkId = myObject:GetComponent(NetworkObject).NetworkId
-- Later, retrieve it
local storedObject = game.GetNetworkObject(networkId)
if storedObject ~= nil then
local retrievedGameObject = storedObject.value
end
Parameters
networkId
Returns
Updated 13 days ago