How to detect number of players in a room?


I’m making a 2-player game over the Internet.

  1. How can first player create a room?
  2. How can 2nd player see that room and join him?
  3. no other player can join?
  4. How can I know when to start the game? For this we need to know number of players in current room.


As sugested by @FortisVenaliter, you should first follow some tutorials or read the manual. This should be enough to cover all of the questions you asked in the the description.

As for the other users who end up here because of the question in the title, you can get the number of players from the NetworkManager.

NetworkManager nm = FindObjectOfType<NetworkManager>();

You should really check out the Learn section of this site. There are 18 networking tutorials there that answer all of your questions.

Here’s a simple implementation. Its ripped from a script I wrote so its not concise. When my players enter the game they are added to List. The Player Setup script on each player determines if the object is the local player or remote player and changes the tag to reflect this. So the server simply Finds the objects and adds them to a List, firstly so I can keep a good handle on each player and secondly, so I can count them. In this case “Player” is the local Client on the Host machine and PlayerRemote is every other connected player. I’ve not shown declarations but I’m sure you’ll get the idea.

function ScanForPlayers()
	yield WaitForSeconds(1);
		var myPlayer = GameObject.FindWithTag("Player");
		var myObject = GameObject.FindGameObjectsWithTag("PlayerRemote");
		for(var i=0;i<myObject.Length;i++)
  •   		yield;*
  •   	}*
  •   	yield;*
  •   	timer = 0; //Reset search timer after last search completes.*
  •   	playerNumbers = playerList.Count;*
  •   	if(playerNumbers > 0)*
  •   	{*
  •   		playersReady = true;*
  •   	}*
  •   	else { playersReady = false; }*
  •   	canScan = true;*
  •   }*
  • }*