Hey, im trying to setup a timer and rounds in my multiplayer game using photon.
I have it working on the master client, but it doesn’t sync on all clients properly.
i have a simplified version of my code, which ill post below.
Thankyou for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class InGameManager : MonoBehaviourPunCallbacks
{
public Text roomTimerText;
public Text roundText;
public int currentRound;
public int MultiplayercurrentRound;
public int totalRounds;
public int Timer;
public int MultiplayerTimer;
public int RoundTime;
public int hostSelectionTime;
public bool flick;
public bool flick2;
public bool startedRound;
public bool startedsync;
public PhotonView PV;
public GameObject pm;
void Awake ()
{
PV = GetComponent<PhotonView>();
}
void OnSceneLoaded ( Scene scene , LoadSceneMode loadSceneMode )
{
if( PhotonNetwork.IsMasterClient )
{
Timer = RoundTime;
StartRound();
}
}
void Update ()
{
if( startedRound && Timer==0 && currentRound==totalRounds )
{
Application.LoadLevel( "Menu" );
}
float minutes = Mathf.FloorToInt(Timer / 60);
float seconds = Mathf.FloorToInt(Timer % 60);
roundText.text = $"{currentRound} / {totalRounds}";
roomTimerText.text = $"{minutes:00}:{seconds:00}";
if( PhotonNetwork.IsMasterClient )
{
startedsync = startedRound;
MultiplayerTimer = Timer;
MultiplayercurrentRound = currentRound;
PV.RPC("SyncTime", RpcTarget.All, MultiplayerTimer);
PV.RPC("SyncRound", RpcTarget.All, MultiplayercurrentRound);
PV.RPC("startRoundbool", RpcTarget.All, startedsync);
if( !flick && startedRound )
{
StartCoroutine( time() );
}
if( !flick2 && !startedRound )
{
StartCoroutine( roundstartdelay() );
}
if( !startedRound && Timer==0 )
{
startedRound = true;
Timer = RoundTime;
}
if( startedRound && Timer==0 )
{
startedRound = false;
StartRound();
currentRound += 1;
}
}
}
[PunRPC]
void SyncTime ( int time )
{
Timer = time;
}
[PunRPC]
void SyncRound ( int round )
{
currentRound = round;
}
[PunRPC]
void startRoundbool ( bool sr )
{
startedRound = sr;
}
void StartRound ()
{
Timer = hostSelectionTime;
}
IEnumerator roundstartdelay ()
{
flick2 = true;
yield return new WaitForSeconds(1);
Timer -= 1;
flick2 = false;
}
IEnumerator time ()
{
flick = true;
yield return new WaitForSeconds(1);
Timer -= 1;
flick = false;
}
}