Issue with Photon View in Unity.

Options
In Update function for the nonlocal player it never gets past if(PhotonView.IsMine). It gets past it every time for the local player. The script with the Update function is called PhotonPlayer and it is on a object instantiated from the Photon Room Script. I will put both down below.

public class PhotonPlayer : MonoBehaviour
{


private PhotonView PV;
//private PhotonView pv;

public GameObject myAvatar;
public int myTeam;

private string myName;

// Start is called before the first frame update
void Start()
{
PV = GetComponent<PhotonView>();
//PV.ViewID = 2;
myName = PV.name;

PV.RPC("RPC_GetTeam", RpcTarget.MasterClient);
//PV.RPC("RPC_SyncHeads", RpcTarget.All, myName);
//RPC_GetTeam();





}

// Update is called once per frame
void Update()
{

if (myAvatar == null && myTeam != 0)
{
if (PV.IsMine)
{
if (myTeam == 1)
{
int spawnPicker = Random.Range(0, GameSetup.GS.spawnPointsTeamOne.Length);

//if (PV.IsMine)
//{
Debug.Log("team1" + myTeam);
myAvatar = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PlayerAvatar"),
GameSetup.GS.spawnPointsTeamOne[spawnPicker].position, GameSetup.GS.spawnPointsTeamOne[spawnPicker].rotation, 0);
Debug.Log("myAvatar" + myAvatar);
//pv = myAvatar.GetComponent<PhotonView>();
//PhotonNetwork.AllocateViewID(pv);
//}
}
else
{
int spawnPicker = Random.Range(0, GameSetup.GS.spawnPointsTeamTwo.Length);
Debug.Log("team2" + myTeam);
//if (PV.IsMine)
//{

myAvatar = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PlayerAvatar"),
GameSetup.GS.spawnPointsTeamTwo[spawnPicker].position, GameSetup.GS.spawnPointsTeamTwo[spawnPicker].rotation, 0);
//pv = myAvatar.GetComponent<PhotonView>();
//PhotonNetwork.AllocateViewID(pv);
//}
}
}
}

}



[PunRPC]
void RPC_GetTeam()
{
myTeam = GameSetup.GS.nextPlayersTeam;
Debug.Log("myTeam:" + myTeam);
GameSetup.GS.UpdateTeam();
PV.RPC("RPC_SentTeam", RpcTarget.OthersBuffered, myTeam);
//RPC_SentTeam(myTeam);
}

[PunRPC]
void RPC_SentTeam(int whichTeam)
{
myTeam = whichTeam;
}

[PunRPC]
void RPC_SyncHeads(string nameIn)
{
GameSetup.GS.playerNames[myTeam] = nameIn;
GameSetup.GS.playerNameTexts[myTeam].text = nameIn;
}
}

And Photon Room where I am instantiating the objects from. I am instantiating in the create player function at the bottom.

public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks
{

public static PhotonRoom room;
private PhotonView PV;
private GameObject netwarkPlayer;
private PhotonView pv;

public bool isGameLoaded;
public int currentScene;

Player[] photonPlayers;
public int playersInRoom;
public int myNumberInRoom;

public int playerInGame;

private bool readyToCount;
private bool readyToStart;
public float startingTime;
private float lessThanMaxPlayers;
private float atMaxPlayer;
private float timeToStart;

private void Awake()
{
if(PhotonRoom.room == null)
{
PhotonRoom.room = this;
}
else
{
if(PhotonRoom.room != this)
{
Destroy(PhotonRoom.room.gameObject);
PhotonRoom.room = this;
}
}
DontDestroyOnLoad(this.gameObject);
}

public override void OnEnable()
{
base.OnEnable();
PhotonNetwork.AddCallbackTarget(this);
SceneManager.sceneLoaded += OnSceneFinishedLoading;
//SceneManager.sceneLoaded += OnSceneFinishedLoading;
//PhotonNetwork.CurrentRoom.IsOpen = false;
}

public override void OnDisable()
{
base.OnDisable();
PhotonNetwork.RemoveCallbackTarget(this);
SceneManager.sceneLoaded -= OnSceneFinishedLoading;
}

// Start is called before the first frame update
void Start()
{
PV = GetComponent<PhotonView>();
readyToCount = false;
readyToStart = false;
lessThanMaxPlayers = startingTime;
atMaxPlayer = 3;
timeToStart = startingTime;

}

// Update is called once per frame
void Update()
{
if(MultiPlayerSettings.multiplayerSetting.delayStart)
{
if(playersInRoom == 1)
{
RestartTimer();
}
if(!isGameLoaded)
{
if(readyToStart)
{
atMaxPlayer -= Time.deltaTime;
lessThanMaxPlayers = atMaxPlayer;
timeToStart = atMaxPlayer;
}
else if(readyToCount)
{
lessThanMaxPlayers -= Time.deltaTime;
timeToStart = lessThanMaxPlayers;
}
//Debug.Log("Display time to start to the players" + timeToStart);
if(timeToStart<= 0)
{
StartGame();
}
}
}
}

public override void OnJoinedRoom()
{
base.OnJoinedRoom();
Debug.Log("We are now in a room");
photonPlayers = PhotonNetwork.PlayerList;
playersInRoom = photonPlayers.Length;
myNumberInRoom = playersInRoom;
PhotonNetwork.NickName = myNumberInRoom.ToString();
if(MultiPlayerSettings.multiplayerSetting.delayStart)
{
Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiPlayerSettings.multiplayerSetting.maxPlayers + ")");
if(playersInRoom > 1)
{
readyToCount = true;
}
if(playersInRoom == MultiPlayerSettings.multiplayerSetting.maxPlayers)
{
readyToStart = true;
if (!PhotonNetwork.IsMasterClient)
return;
PhotonNetwork.CurrentRoom.IsOpen = false;
}
}
else
{
StartGame();
}
}

public override void OnPlayerEnteredRoom(Player newPlayer)
{
base.OnPlayerEnteredRoom(newPlayer);
Debug.Log("A new player has joined the room");
photonPlayers = PhotonNetwork.PlayerList;
playersInRoom++;
if(MultiPlayerSettings.multiplayerSetting.delayStart)
{
Debug.Log("Displayer players in room out of max players possible (" + playersInRoom + ":" + MultiPlayerSettings.multiplayerSetting.maxPlayers + ")");
if(playersInRoom > 1)
{
readyToCount = true;
}
if(playersInRoom == MultiPlayerSettings.multiplayerSetting.maxPlayers)
{
readyToStart = true;
if (!PhotonNetwork.IsMasterClient)
return;
PhotonNetwork.CurrentRoom.IsOpen = false;
}
}
}

void StartGame()
{
isGameLoaded = true;
if (!PhotonNetwork.IsMasterClient)
return;
if(MultiPlayerSettings.multiplayerSetting.delayStart)
{
PhotonNetwork.CurrentRoom.IsOpen = false;
}
PhotonNetwork.LoadLevel(MultiPlayerSettings.multiplayerSetting.multiplayerScene);
}

void RestartTimer()
{
lessThanMaxPlayers = startingTime;
timeToStart = startingTime;
atMaxPlayer = 3;
readyToCount = false;
readyToStart = false;
}

void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
{
currentScene = scene.buildIndex;
if(currentScene == MultiPlayerSettings.multiplayerSetting.multiplayerScene)
{
isGameLoaded = true;

if(MultiPlayerSettings.multiplayerSetting.delayStart)
{

PV.RPC("RPC_LoadedGameScene", RpcTarget.MasterClient);

}
else
{
RPC_CreatePlayer();
/* if (PV.IsMine)
{
PV.RPC("RPC_CreatePlayer", RpcTarget.All);
}*/

}
}
}

[PunRPC]
private void RPC_LoadedGameScene()
{
playerInGame++;

if(playerInGame == PhotonNetwork.PlayerList.Length)
{

//RPC_CreatePlayer();
PV.RPC("RPC_CreatePlayer", RpcTarget.All);


}
}

[PunRPC]
private void RPC_CreatePlayer()
{
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonNetworkPlayer"), transform.position, Quaternion.identity, 0);
//pv = netwarkPlayer.GetComponent<PhotonView>();
//PhotonNetwork.AllocateViewID(pv);

}

public override void OnPlayerLeftRoom(Player otherPlayer)
{
base.OnPlayerLeftRoom(otherPlayer);
Debug.Log(otherPlayer.NickName + "has left the game");
playersInRoom--;
}

}

Comments

  • jeanfabre
    Options
    Hi,

    It looks to me a problem of logic and not a problem is the ismine flag itself. I would properly debug this with logs or public fields visible in the inspector.

    are you sure for example that if (myAvatar == null && myTeam != 0) resolved to true and that myteam is 1 shortly after?

    Bye,

    Jean

  • benmoen
    Options
    Thank you for answering, I can confirm that myAvatar == null and myTeam != 0 are true. When I remove if(PhotonVIew.isMine) the player instantiates but it's owner is the local player and not the non local player. It feels like the non local player isn't even running the script.
  • jeanfabre
    Options
    Hi,

    You should not try to instantiate network object yourself, always use PhotonNetwork.Instantiate() for this.

    Bye,

    Jean
  • benmoen
    Options
    I am slightly confused by this because I did use PhotonNetwork.Instantiate both times. Could the problem be with the standalone. Is there a setting I might not know about?