- Home /
uNet: Waiting for two players to connect?
Hi Guys, I am making a 1v1 LAN space shooter and right now I am trying to program the create game/join game functionality. I have made the create game/join game functionality but to make it better I want it so that when the 'create game' button is clicked, the host's player isn't immediately spawned in (like the code does below) but the host waits for another client to connect and then starts the game, spawning both the host player and client player in at the same time. Here is my current code:
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class NetUI : NetworkDiscovery {
[SerializeField]
Button create;
[SerializeField]
Button join;
// Use this for initialization
void Awake() {
create.onClick.AddListener(CreateClick);
join.onClick.AddListener(JoinClick);
Initialize();
}
void CreateClick()
{
NetworkManager.singleton.networkPort = 7777;
StartAsServer();
NetworkManager.singleton.StartHost();
gameObject.SetActive(false);
}
void JoinClick()
{
StartAsClient();
}
public override void OnReceivedBroadcast(string fromAddress, string data)
{
NetworkManager.singleton.networkPort = 7777;
NetworkManager.singleton.networkAddress = fromAddress;
NetworkManager.singleton.StartClient();
StopBroadcast();
gameObject.SetActive(false);
}
}
What do I add to this code so that when create is clicked, the server is initialized, and once the client joins, the game begins. Thank you!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
c# Networking How can I check if there is already a server 0 Answers
how to find out if player hosted game 1 Answer
Multiple Cars not working 1 Answer