Not able to Instantiate Player Prefab

Options
using UnityEngine;
using Photon;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class Menu : Photon.PunBehaviour 
{
	public Transform namePanel;
	public Transform lobbyPanel;
	//public Transform HUD;

	public InputField editName;
	public InputField editRoomName;
	public Text	messages;
	public Text loggedInUser;
	public Text myRoom;

	private List<RoomJoiner> rooms = new List<RoomJoiner>();
	private string roomName = "DEFAULT ROOM NAME";

	void Start () 
	{
		
	}

	void Update () 
	{
		
	}
		
	public void Lobby()
	{
		PhotonNetwork.playerName = editName.text;
		PhotonNetwork.ConnectUsingSettings("v1.0");
		messages.text = "Connecting...";
		loggedInUser.text = PhotonNetwork.playerName;
	}
		
	public override void OnConnectedToMaster()
	{
		PhotonNetwork.JoinLobby ();
		messages.text = "Entering lobby...";
	}
		
	public override void OnJoinedLobby()
	{
		Debug.Log("Inside the Lobby");
		namePanel.gameObject.SetActive (false);
		messages.gameObject.SetActive(false);
		lobbyPanel.gameObject.SetActive (true);
		//PhotonNetwork.JoinRandomRoom ();
	}

	public void CreateGame () 
	{
		roomName = editRoomName.text;

		if (roomName != "") 
		{
			RoomOptions roomOptions = new RoomOptions () {IsVisible = true, MaxPlayers = 5};
			PhotonNetwork.CreateRoom(roomName, roomOptions, TypedLobby.Default);
			lobbyPanel.gameObject.SetActive (false);
		}
	}

	public override void OnPhotonCreateRoomFailed(object[] codeMessage)
	{
		if((short) codeMessage [0] == ErrorCode.GameIdAlreadyExists)
		{
			PhotonNetwork.room.Name += "-2";
			CreateGame ();
		}
	}

	private bool checkSameNameOnPlayersList(string name) 
	{
		foreach (PhotonPlayer pp in PhotonNetwork.otherPlayers) 
		{
			if (pp.NickName.Equals(name)) 
			{
				return true;
			}
		}

		return false;
	}

	public override void OnCreatedRoom () 
	{
		Debug.Log ("Room Created");
		messages.gameObject.SetActive(false);
	}
		
	public override void OnReceivedRoomListUpdate () 
	{
		foreach (RoomJoiner roomBtn in rooms) 
		{
			Destroy(roomBtn.gameObject);
		}
		rooms.Clear ();

		float i = -120f;
		foreach (RoomInfo room in PhotonNetwork.GetRoomList()) 
		{
			if (!room.IsOpen)
				continue;

			GameObject buttonPrefab = Resources.Load<GameObject>("JoinRoomBtn");
			GameObject roomButton = Instantiate<GameObject>(buttonPrefab);
			roomButton.GetComponent<RoomJoiner>().RoomName = room.name;

			string info = room.Name.Trim() + " (" + room.PlayerCount + "/" + room.MaxPlayers + ")";
			myRoom.text = info;
			rooms.Add(roomButton.GetComponent<RoomJoiner>());
			roomButton.transform.SetParent (lobbyPanel, false);
			i += -50f;
		}
	}

	public override void OnJoinedRoom()
	{
		Debug.Log ("Room Joined");

		if (checkSameNameOnPlayersList (PhotonNetwork.playerName)) 
		{
			string newName = PhotonNetwork.playerName;
			do 
			{
				newName += "-2";
			}while (checkSameNameOnPlayersList (newName));

			PhotonNetwork.playerName = newName;
		}

		lobbyPanel.gameObject.SetActive (false);
		//HUD.gameObject.SetActive (true);
		PhotonNetwork.isMessageQueueRunning = false;
		photonView.RPC ("LoadGame", PhotonTargets.All);
	}
		
	[PunRPC]
	public void LoadGame () 
	{
		PhotonNetwork.LoadLevel("GamePlay");
	}

}
This is my Menu Class which is attached to Scene 1. This class provides a lobby for the user to create and join the room of their choice which is working fine.

Comments

  • using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using Photon;
    using UnityEngine.UI;
    
    public class GamePlayManager : Photon.PunBehaviour {
    
    	//GENERATE A HAND FOR EACH PLAYER IN GAME. MAKE SURE THERE ARE 5 COMM CARDS.
    	//COMPARE THEM AND ADD POINTS TO THE WINNER
    
    	private PhotonView gpmPhotonView;
    
    	private PhotonView myPhotonView;
    
    	//these are the players that are present in the current game, identified by player number
    	public static List<int> playerIDs = new List<int>();
    
    	//use this to determine who comes next
    	public static List<Player> playerList = new List<Player>();
    
    	public static List<string> commCards;
    
    	static int indexOfShuffledDeck;
    
    	static List<List<string>> twoCardLists;
    
    	PhotonPlayer pp;
    
    	//only populated if get to showdown. Used to add points and win to winner(s)
    	static List<Player> winningPlayers;
    
    	//connect
    	void Start () {
    		//PhotonNetwork.ConnectUsingSettings("v1.0");
    		PhotonNetwork.isMessageQueueRunning = true;
    		gpmPhotonView = this.GetComponent<PhotonView> ();
    		Debug.Log ("Game Started");
    		CreatePlayer ();
    		//when game starts, the slider, slider value text, and confirm bet is inactive. 
    		//Slider is activated when bet/raise button is pressed
    		GameObject.Find("BetSlider").SetActive(false);
    		GameObject.Find ("SliderValText").GetComponent<Text> ().text = "";
    		GameObject.Find ("ConfirmBetButton").SetActive (false);
    
    		//TODO: SET MENU BUTTONS HERE? 
    	}
    
    	void OnGUI()
    	{
    		GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    
    	}
    
    
    
    	//runs only when non-local player enters
    	public override void OnPhotonPlayerConnected(PhotonPlayer photonPlayer)
    	{
    		pp = photonPlayer;
    
    		Debug.Log ("OnPhotonPlayerConnected: " + photonPlayer);
    		print("new player ID: "+photonPlayer.ID);
    
    		Text otherPlayerText = GameObject.Find ("OtherPlayerNumber").GetComponent<Text> ();
    
    		otherPlayerText.text = "new player ID: "+photonPlayer.ID;
    
    		playerIDs.Add (photonPlayer.ID); 
    		;
    
    		}
    
    	}
    
    	bool gameStarted = false;
    
    	void Update() {
    		//add the non-local player to playerList when he joins
    
    		if (PhotonNetwork.inRoom) 
    		{
    			//Debug.Log ("hahahahahha");
    			Debug.Log ("Update Count of Players "+playerList.Count);
    			Debug.Log ("Room Player Count "+PhotonNetwork.room.PlayerCount);
    			//Debug.Log ("1");
    			Player otherPlayer = GameObject.Find ("Player").GetComponent<Player> ();
    
    			otherPlayer.ID = pp.ID;
    
    			playerList.Add (otherPlayer);
    		}
    
    		if (PhotonNetwork.room.PlayerCount > 1 && gameStarted == false) {
    		
    			gameStarted = true;
    			StartGame ();
    		}
    	
    	}
    
    	//when local player joins the room
    	void CreatePlayer()
    	{
    		Debug.Log ("Local Player Joined the Room");
    		Debug.Log ("Number of Players in Room "+ PhotonNetwork.playerList.Length);
    		if (PhotonNetwork.playerList.Length > 1) {
    
    			Debug.Log ("2");
    			Text otherPlayerText = GameObject.Find ("OtherPlayerNumber").GetComponent<Text> ();
    			otherPlayerText.text = "Other player already in room with ID: "+PhotonNetwork.playerList[0].ID;
    
    			//the first player in game
    			Player firstPlayer = GameObject.Find ("Player(Clone)").GetComponent<Player> ();
    
    			//TEMP HARDCODED
    			firstPlayer.ID = 1;
    			Debug.Log ("Testing1122111");
    			//playerList.Add (firstPlayer);
    			StartGame ();
    		}
    		Debug.Log ("Testing11111");
    		print ("player with ID "+PhotonNetwork.player.ID+" joined room");
    
    		Text myPlayerText = GameObject.Find ("MyPlayerNumber").GetComponent<Text> ();
    
    		myPlayerText.text = "player with ID " + PhotonNetwork.player.ID + " joined room";
    
    		//TODO: HOW DO I USE PHOTON PLAYERS WITH MY OWN PLAYER PROPERTIES???
    		//for now I am creating a list of Players (script type) with the same IDs as photonPlayers
    		print("Number of Photon Players: "+ PhotonNetwork.playerList.Length);
    
    		//instantiate the player object that just joined (this player needs to have an associated ID)
    		GameObject playerGO = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
    
    		Player playerScript = playerGO.GetComponent<Player> ();
    		Debug.Log ("Testing");
    //		myPhotonView = playerScript.GetComponent<PhotonView> ();
    
    		playerScript.ID = PhotonNetwork.player.ID;
    
    		//adding this player to static list GamePlayManager.playerList
    		//TODO: SYNC THIS LIST ACROSS THE NETWORK
    		playerList.Add (playerScript);
    
    		if (PhotonNetwork.playerList.Length > 1) {
    		
    			StartGame ();
    		}
    	}
    		
    	public static void StartGame () {
    		Debug.Log ("GAME STARTED NOW");
    		print ("number of players in playerList is " + playerList.Count);
    
    		//TODO: ANIMATE THE SHUFFLING DECK WITH SOUND
    		//SYNC THIS ACROSS THE NETWORK
    		GameState.shuffledDeck = ShuffleDeck();
    
    		//game starts at preDeal round, ASK STRADDLE FOR BET
    		GameState.currentRound = GameState.Rounds.isPreDeal;
    
    		//TODO: INCREMENT DEALER SINCE LAST GAME 
    		GameState.dealer = playerList [0];
    
    		//assigns the small blind player, big blind player and straddle player based on the dealer
    		GameState.OnGameStarted ();
    
    		//small blind
    		GameState.smallBlindPlayer.myBetAmount = GameState.smallBlindAmount;
    		GameState.smallBlindPlayer.myChipAmount -= GameState.smallBlindPlayer.myBetAmount;
    
    		//big blind
    		GameState.bigBlindPlayer.myBetAmount = GameState.bigBlindAmount;
    		GameState.bigBlindPlayer.myChipAmount -= GameState.bigBlindPlayer.myBetAmount;
    
    		GameState.currentPlayer = GameState.straddlePlayer;
    
    		//TODO: SHOW BUTTONS FOR CURRENT PLAYER
    
    //----------DEALING THE CARDS TO PLAYERS---------------------------// 
    
    		//TODO: MOVE THESE TO STRADDLE/PASS STRADDLE
    		GenerateTwoCardHands ();
    
    		//generate the community cards to add to 7 card hands
    		GenerateCommCards ();
    
    		//create the hand object for each player (also creates player.hand.twoCardList)
    		GeneratePlayerHands ();
    
    		//ONLY CALL IF IT GETS TO SHOWDOWN
    		AddPointsToWinners ();
    
    	}
    				
    }
    This my Gameplay manager class which is supposed to detect the player that has joined the game and once there are more than 1 player in the room the game is supposed to be started. But for some reason I am not able to Instantiate the player prefab located in resources folder with PhotonView attached to it.
  • using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using Photon;
    using UnityEngine.UI;
    
    public class GamePlayManager : Photon.PunBehaviour {
    
    	//GENERATE A HAND FOR EACH PLAYER IN GAME. MAKE SURE THERE ARE 5 COMM CARDS.
    	//COMPARE THEM AND ADD POINTS TO THE WINNER
    
    	private PhotonView gpmPhotonView;
    
    	private PhotonView myPhotonView;
    
    	//these are the players that are present in the current game, identified by player number
    	public static List<int> playerIDs = new List<int>();
    
    	//use this to determine who comes next
    	public static List<Player> playerList = new List<Player>();
    
    	public static List<string> commCards;
    
    	static int indexOfShuffledDeck;
    
    	static List<List<string>> twoCardLists;
    
    	PhotonPlayer pp;
    
    	//only populated if get to showdown. Used to add points and win to winner(s)
    	static List<Player> winningPlayers;
    
    	//connect
    	void Start () {
    		//PhotonNetwork.ConnectUsingSettings("v1.0");
    		PhotonNetwork.isMessageQueueRunning = true;
    		gpmPhotonView = this.GetComponent<PhotonView> ();
    		Debug.Log ("Game Started");
    		//CreatePlayer ();
    		//when game starts, the slider, slider value text, and confirm bet is inactive. 
    		//Slider is activated when bet/raise button is pressed
    		GameObject.Find("BetSlider").SetActive(false);
    		GameObject.Find ("SliderValText").GetComponent<Text> ().text = "";
    		GameObject.Find ("ConfirmBetButton").SetActive (false);
    
    		//TODO: SET MENU BUTTONS HERE? 
    	}
    
    	void OnGUI()
    	{
    		GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    
    	}
    	/*
    	//join a random room
    	public override void OnJoinedLobby()
    	{
    		RoomOptions roomOptions = new RoomOptions() { isVisible = false, maxPlayers = 9 };
    		PhotonNetwork.JoinOrCreateRoom("Room", roomOptions, TypedLobby.Default);
    	}*/
    
    	//runs only when non-local player enters
    	public override void OnPhotonPlayerConnected(PhotonPlayer photonPlayer)
    	{
    		pp = photonPlayer;
    
    		Debug.Log ("OnPhotonPlayerConnected: " + photonPlayer);
    		print("new player ID: "+photonPlayer.ID);
    
    		Text otherPlayerText = GameObject.Find ("OtherPlayerNumber").GetComponent<Text> ();
    
    		otherPlayerText.text = "new player ID: "+photonPlayer.ID;
    
    		playerIDs.Add (photonPlayer.ID); 
    		;
    		//I PUT THIS IN UPDATE FUNCTION TEMPORARILY
    		if (PhotonNetwork.playerList.Length > 1) {
    
    			//DOESN'T WORK
    			//			Player otherPlayer = GameObject.Find ("Player(Clone)").GetComponent<Player> ();
    
    		}
    
    	}
    
    	bool gameStarted = false;
    
    	void Update() {
    		//add the non-local player to playerList when he joins
    		/*while (PhotonNetwork.playerList.Length < 2) {
    
    			Debug.Log ("Update Count of Players "+playerList.Count);
    			Debug.Log ("1");
    			Player otherPlayer = GameObject.Find ("Player").GetComponent<Player> ();
    
    			otherPlayer.ID = pp.ID;
    
    			playerList.Add (otherPlayer);
    		}*/
    
    		if (PhotonNetwork.inRoom) 
    		{
    			//Debug.Log ("hahahahahha");
    			Debug.Log ("Update Count of Players "+playerList.Count);
    			Debug.Log ("Room Player Count "+PhotonNetwork.room.PlayerCount);
    			//Debug.Log ("1");
    			Player otherPlayer = GameObject.Find ("Player").GetComponent<Player> ();
    
    			otherPlayer.ID = pp.ID;
    
    			playerList.Add (otherPlayer);
    		}
    
    		if (PhotonNetwork.room.PlayerCount > 1 && gameStarted == false) {
    		
    			gameStarted = true;
    			StartGame ();
    		}
    	
    	}
    
    	//when local player joins the room
    	void OnJoinedRoom()//11111111111111111111111
    	{
    		Debug.Log ("Local Player Joined the Room");
    		Debug.Log ("Number of Players in Room "+ PhotonNetwork.playerList.Length);
    		if (PhotonNetwork.playerList.Length > 1) {
    
    			Debug.Log ("2");
    			Text otherPlayerText = GameObject.Find ("OtherPlayerNumber").GetComponent<Text> ();
    			otherPlayerText.text = "Other player already in room with ID: "+PhotonNetwork.playerList[0].ID;
    
    			//the first player in game
    			Player firstPlayer = GameObject.Find ("Player(Clone)").GetComponent<Player> ();
    
    			//TEMP HARDCODED
    			firstPlayer.ID = 1;
    			Debug.Log ("Testing1122111");
    			//playerList.Add (firstPlayer);
    			StartGame ();
    		}
    		Debug.Log ("Testing11111");
    		print ("player with ID "+PhotonNetwork.player.ID+" joined room");
    
    		Text myPlayerText = GameObject.Find ("MyPlayerNumber").GetComponent<Text> ();
    
    		myPlayerText.text = "player with ID " + PhotonNetwork.player.ID + " joined room";
    
    		//TODO: HOW DO I USE PHOTON PLAYERS WITH MY OWN PLAYER PROPERTIES???
    		//for now I am creating a list of Players (script type) with the same IDs as photonPlayers
    		print("Number of Photon Players: "+ PhotonNetwork.playerList.Length);
    
    		//instantiate the player object that just joined (this player needs to have an associated ID)
    		GameObject playerGO = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
    
    		Player playerScript = playerGO.GetComponent<Player> ();
    		Debug.Log ("Testing");
    //		myPhotonView = playerScript.GetComponent<PhotonView> ();
    
    		playerScript.ID = PhotonNetwork.player.ID;
    
    		//adding this player to static list GamePlayManager.playerList
    		//TODO: SYNC THIS LIST ACROSS THE NETWORK
    		playerList.Add (playerScript);
    
    		if (PhotonNetwork.playerList.Length > 1) {
    		
    			StartGame ();
    		}
    	}
    		
    	public static void StartGame () {
    		Debug.Log ("GAME STARTED NOW");
    		print ("number of players in playerList is " + playerList.Count);
    
    		//TODO: ANIMATE THE SHUFFLING DECK WITH SOUND
    		//SYNC THIS ACROSS THE NETWORK
    		GameState.shuffledDeck = ShuffleDeck();
    
    		//game starts at preDeal round, ASK STRADDLE FOR BET
    		GameState.currentRound = GameState.Rounds.isPreDeal;
    
    		//TODO: INCREMENT DEALER SINCE LAST GAME 
    		GameState.dealer = playerList [0];
    
    		//assigns the small blind player, big blind player and straddle player based on the dealer
    		GameState.OnGameStarted ();
    
    		//small blind
    		GameState.smallBlindPlayer.myBetAmount = GameState.smallBlindAmount;
    		GameState.smallBlindPlayer.myChipAmount -= GameState.smallBlindPlayer.myBetAmount;
    
    		//big blind
    		GameState.bigBlindPlayer.myBetAmount = GameState.bigBlindAmount;
    		GameState.bigBlindPlayer.myChipAmount -= GameState.bigBlindPlayer.myBetAmount;
    
    		GameState.currentPlayer = GameState.straddlePlayer;
    
    		//TODO: SHOW BUTTONS FOR CURRENT PLAYER
    
    //----------DEALING THE CARDS TO PLAYERS---------------------------// 
    
    		//TODO: MOVE THESE TO STRADDLE/PASS STRADDLE
    		GenerateTwoCardHands ();
    
    		//generate the community cards to add to 7 card hands
    		GenerateCommCards ();
    
    		//create the hand object for each player (also creates player.hand.twoCardList)
    		GeneratePlayerHands ();
    
    		//ONLY CALL IF IT GETS TO SHOWDOWN
    		AddPointsToWinners ();
    
    	}
    This my GamePlayeManager Script which is attached to Scene 2. This script is supposed to be checking all the players that joined the room and when there is more then 1 player in the room the game is supposed to be started. But for some reason it is neither starting the nor Instantiating my Player Prefab which is located in Resources folder with a PhotonView attached to it.