I’m following a PUN tutorial and it’s been frustrating me quite a bit, I’m going through videos by InfoGamer and what he says makes sense but for the life of me I can’t get PhotonNetwork.Instantiate to work despite me copying what he’s done almost exactly. He uses void Start () to instantiate the player when you have joined a room but when I click play nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class QuickStartRoomController : MonoBehaviourPunCallbacks
{
[SerializeField]
private int multiplayerSceneIndex;
public override void OnEnable()
{
PhotonNetwork.AddCallbackTarget(this);
}
public override void OnDisable()
{
PhotonNetwork.RemoveCallbackTarget(this);
}
public override void OnJoinedRoom()
{
StartGame();
}
public void StartGame()
{
if (PhotonNetwork.IsMasterClient)
{
PhotonNetwork.LoadLevel(multiplayerSceneIndex);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
public class QuickStartLobbyController : MonoBehaviourPunCallbacks
{
[SerializeField]
private GameObject quickStartButton;
[SerializeField]
private GameObject quickCancelButton;
[SerializeField]
private int RoomSize;
private void Start()
{
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
PhotonNetwork.AutomaticallySyncScene = true;
quickStartButton.SetActive(true);
}
public void QuickStart()
{
quickStartButton.SetActive(false);
quickCancelButton.SetActive(true);
PhotonNetwork.JoinRandomRoom();
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
CreateRoom();
}
public void QuickCancel ()
{
quickCancelButton.SetActive(false);
quickStartButton.SetActive(true);
PhotonNetwork.LeaveRoom();
}
public void CreateRoom()
{
int randomRoomNumber = Random.Range(0, 10000);
RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)RoomSize };
PhotonNetwork.CreateRoom("Room" + randomRoomNumber, roomOps);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class GameSetup : MonoBehaviour
{
public static GameSetup GS;
public Transform[] spawnPoints;
private void OnEnable()
{
if (GameSetup.GS == null)
{
GameSetup.GS = this;
Debug.Log("Game Setup Empty Ready");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using System.IO;
using Photon.Realtime;
using UnityEngine.UI;
public class PhotonPlayer : MonoBehaviour
{
public GameObject localPlayer;
void Start()
{
CreatePlayer();
}
private void CreatePlayer()
{
int spawnPicker = Random.Range(0, GameSetup.GS.spawnPoints.Length);
localPlayer = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "FPSControllerEmptyPrefab"), GameSetup.GS.spawnPoints[spawnPicker].position, GameSetup.GS.spawnPoints[spawnPicker].rotation, 0);
}
}
Just to make sure I wasn’t being dumb I made sure because the tutorial uses paths to define where the prefabs are I deleted any other resource folders I could find thinking that was causing the problem and so on but still the players don’t instantiate across the network.
I’m not doing anything complicated right now, just connecting to a random room and having a player spawn in. I have posted in other places like the PUN support forums but honestly they seem rather dead people just don’t seem to use them very much.
I’ve just realised I am actually a retard and I completely missed out a step involving attaching the PhotonPlayer script to the GameSetup object so that everything spawns automatically and now it’s working. Interestingly though he missed out this step on the player avatar video he did so I wonder if this was a correction but I’ll double check all that and see.
My only problem now if anyone is curious is that I’ve come onto the same problem again of my two players for some reason copying each other. This shouldn’t be the case with the code that I have now that I think about it more the code should all be correct and I’m wondering if the standalone client is reading my input no matter what even if I’ve clicked onto the editor.
Has anyone else run into this as an issue? It’s another thing that’s made testing annoying and I want to make sure my networking is right before I move on.
Did some testing and I found out it was in fact the network movement that was the issue and the players weren’t moving separate from each other looks like I need to get researching on that.
I can’t believe this actually worked, I hate it when I make a really length post and research all the fixes myself. So it turns out that for whatever reason at least in the latest version of PUN 2 PhotonView.IsMine does not work in the update function and I have no idea why. If somebody could explain it to me that would be great, but what I did was I found this answer which seemed incredibly unlikely but it worked for me and I simply wrapped all of my functions in separate IsMine if statements and now it seems to all be working flawlessly with no movement at all from the other player when there shouldn’t be.
Why does nobody make any mention of this in the tutorials or the documentation? I’m actually really infuriated lol.
For newbies as well and anybody else getting annoyed with PUN, here’s the code I ended up with try this and see if it fixes your problems too.
Edit: NOPE! Still unresolved! Going to have to do more testing and see if any other solutions can be found That’s really annoying unless I’m doing something weird.