I want a game manager, to randomly choose one player in the room, to be the chosen one. Then i want the chosen player, to get displayed on his screen, that he is the chosen one, and activate a “Chosen” bool on his playermanager.
Thank you for taking your time.
All help is appreciated
using System;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
public void ChoosePlayer()
{
if (PhotonNetwork.MasterClient.IsMasterClient)
{
//Decide what number
int RandomNumber = UnityEngine.Random.Range(1,PhotonNetwork.PlayerList.Length);
Debug.Log("The random number is " + RandomNumber);
//Temporary, See all the ActorNumbers for all Players.
foreach (Photon.Realtime.Player players in PhotonNetwork.PlayerList)
{
print(players.ActorNumber);
}
Debug.Log(PhotonNetwork.CurrentRoom.GetPlayer(RandomNumber) + " is chosen");
ChosenPlayer = RandomNumber;
UpdateGameState(GameState.DisplayRoles);
}
else
{
return;
}
}
public void DisplayRoles()
{
RoleText.gameObject.SetActive(true);
if (PhotonNetwork.LocalPlayer.ActorNumber == ChosenPlayer)
{
RoleText.text = ("You are the chosen player");
RoleText.color = Color.red;
}
else
{
RoleText.text = ("You are normal");
RoleText.color = Color.green;
}
}
using UnityEngine;
using Photon.Pun;
using System.IO;
public class PlayerManager : MonoBehaviour
{
PhotonView PV;
public bool isChosen;
int ChosenPlayer;
private void Awake()
{
PV = GetComponent<PhotonView>();
}
private void Start()
{
if (PV.IsMine)
{
CreateController();
}
ChosenPlayer = FlowManager.Instance.ChosenPlayer;
}
public void Update()
{
PV.RPC("UpdatePlayerBool", RpcTarget.AllViaServer);
}
[PunRPC]
public void UpdatePlayerBool()
{
if (PV.IsMine)
{
if (PhotonNetwork.LocalPlayer.ActorNumber == ChosenPlayer)
{
isChosen = true;
}
else
{
return;
}
}
}
void CreateController()
{
PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PlayerController"), Vector3.zero, Quaternion.identity);
}
}
- I expected this to work.
- This didnt work, I think each of the clients generates their own number, and if the number is the same as their actornumber, they will get displayed that he is the chosen one. This does not work for the bool tho, only the masterclient bool will be true if he is the chosen one.
The manager is being controlled/owned by the masterclient.