Can't get photon to spawn players, or join the same room.
I'm making an agar.io like copy for a project to gain experience with unity.
I'm using Photon to manage networking. I have a NetworkManager script below, that is active in my scene and setup correctly, with player prefab (in resources folder), room name, and spawnpoint. using UnityEngine; using System.Collections;
public class NetworkManager : MonoBehaviour {
const string VERSION = "1";
public string roomName = "ffa";
public string playerPrefabName = "Player";
public Transform spawnPoint;
void Start () {
PhotonNetwork.ConnectUsingSettings(VERSION);
Debug.Log("NetworkManager Started");
}
void onJoinedLobby() {
RoomOptions roomOptions = new RoomOptions() { IsVisible = false, maxPlayers = 4 };
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
isConnected();
}
void onJoinedRoom() {
PhotonNetwork.Instantiate(playerPrefabName, spawnPoint.position, spawnPoint.rotation, 0);
isConnected();
}
void isConnected() {
if(PhotonNetwork.connected == true) {
Debug.Log("connected");
} else {
Debug.Log("not connected");
}
}
}
Now, my issue seems to be that overall onJoinedLobby, and onJoinedRoom, aren't ever firing, as I don't get my debug messages, while I do get the one from Start(). Another issue is that when I remove my player prefab from the scene like I should, and start my game, it never spawns, of course that is because neither of my functions are ever firing, because my client never seems to join a room/lobby.
Also getting a debug message in chat that I can't seem to find any info on why it's happening from google or docs.
SwitchToProtocol: Udp PhotonNetwork.connected: False
UnityEngine.Debug:Log(Object)
PhotonNetwork:SwitchToProtocol(ConnectionProtocol) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1172)
PhotonNetwork:ConnectUsingSettings(String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1219)
NetworkManager:Start() (at Assets/Assets/NetworkManager.cs:12)
Any help? First time messing with Photon or really unity anyways. Thanks.
Answer by tobiass · Sep 05, 2016 at 09:33 AM
There is no error in your logs. We can't debug your code. Have a look at the ConnectAndJoinRandom.cs. If this also fails, find the PhotonServerSettings file and delete it. The PUN Wizard should pop up and create a new one. Insert your AppId from the Dashboard.
I reset the PUN settings and actually created an entire new project twice, didn't fix it.
What seemed to be the issue was simply how I was handling the status. I don't really have the best internet so, sometimes things take a bit to process, which was what was happening. It was starting to connect but didn't finalize it. Here is the script I finally got to work (at midnight :P) using UnityEngine; using System.Collections;
public class NewNetworkScript : $$anonymous$$onoBehaviour {
public string status;
public string roomName;
public string playerPrefabName = "StarterBall";
public Transform spawnPoint;
private int counter = 1;
void Start () {
Connect();
InvokeRepeating("UpdateStatus", 2, 1);
}
void Connect() {
PhotonNetwork.ConnectUsingSettings("v001");
}
void UpdateStatus() {
status = PhotonNetwork.connectionStateDetailed.ToString();
if(status == "JoinedLobby") {
RoomOptions roomOptions = new RoomOptions();
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
}
if(status == "Joined" && counter == 1) {
onJoinedRoom();
counter = 2;
}
}
void onJoinedRoom() {
PhotonNetwork.Instantiate(playerPrefabName, spawnPoint.position, spawnPoint.rotation, 0);
}
}
Although, I do have a question regarding PhotonView.is$$anonymous$$ine and meshes (colors on my spheres in a agar.io like game)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ColorChanger : $$anonymous$$onoBehaviour {
public List<$$anonymous$$aterial> $$anonymous$$ats = new List<$$anonymous$$aterial>();
public PhotonView pv;
void Start() {
pv = PhotonView.Get(this);
}
void Awake() {
if(pv.is$$anonymous$$ine) {
GetComponent<Renderer>().material = $$anonymous$$ats[Random.Range(0, $$anonymous$$ats.Count)];
}
}
}
Seems to kinda work. For example I'll spawn in on the editor as red, then I'll open the app on my pc and it'll spawn as black, which is what I see on the editor. but then in my app I see my original as black ins$$anonymous$$d of red, and then the new app one is green.
Any help? Sorry if I'm confusing. @tobiass