Search Unity

Third Party [PHOTON] Merry Fragmas part 3 problems

Discussion in 'Multiplayer' started by Moddwyn, Dec 31, 2016.

  1. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    I did what the video did but i having some problems. https://unity3d.com/learn/tutorials/topics/asset-store/merry-fragmas-multiplayer-fps-part-3
    1. When a player spawns, the message doesn't add a new line, it was just a continuous string.
    2. The killed message doesn't show
    3. Room list doesn't update/show (I spelled the function right)
    I get this error when someone joins my room: (My gamobject where the NetworkManager is attached to have a PhotonView because it did so in the video and no i get this error.)


    My Scripts:
    NetworkManager

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Security.Cryptography;
    4. using UnityEngine.UI;
    5. using System.Collections.Generic;
    6.  
    7. public class NetworkManager : MonoBehaviour {
    8.  
    9.     public string versionNum = "2.0";
    10.     public GameObject point;
    11.     [SerializeField] Text connectionText;
    12.     [SerializeField] Transform[] spawnPoints;
    13.     [SerializeField] Camera sceneCamera;
    14.  
    15.     public GameObject customGameWindow;
    16.     public InputField nameInput;
    17.     public InputField roomToJoin;
    18.     public InputField roomList;
    19.     public InputField messageWindow;
    20.  
    21.     GameObject player;
    22.  
    23.     Queue<string> messages;
    24.     const int messageCount = 6;
    25.     PhotonView photonView;
    26.  
    27.     void Start () {
    28.         messageWindow.gameObject.SetActive (false);
    29.         messages = new Queue<string> (messageCount);
    30.         photonView = GetComponent <PhotonView>();
    31.  
    32.         PhotonNetwork.logLevel = PhotonLogLevel.Full;
    33.         PhotonNetwork.ConnectUsingSettings ("0.2");
    34.         point.SetActive (false);
    35.     }
    36.  
    37.     void Update () {
    38.         connectionText.text = PhotonNetwork.connectionStateDetailed.ToString ();
    39.     }
    40.  
    41.     void OnConnectedToMaster()
    42.     {
    43.         connectionText.gameObject.SetActive (false);
    44.         customGameWindow.SetActive (true);
    45.     }
    46.  
    47.     void OnReceivedRoomListUpdate()
    48.     {
    49.         Debug.Log ("room list updated");
    50.         roomList.text = "";
    51.         RoomInfo[] rooms = PhotonNetwork.GetRoomList ();
    52.         foreach(RoomInfo room in rooms)
    53.             roomList.text += room.name + "\n";
    54.     }
    55.  
    56.     public void JoinRoom(){
    57.         PhotonNetwork.player.name = nameInput.text;
    58.         RoomOptions ro = new RoomOptions () { IsVisible = true, MaxPlayers = 10 };
    59.         PhotonNetwork.JoinOrCreateRoom (roomToJoin.text, ro, TypedLobby.Default);
    60.         customGameWindow.SetActive (false);
    61.         connectionText.gameObject.SetActive (true);
    62.     }
    63.  
    64.     void OnJoinedRoom() {
    65.         StartSpawnProcess (0f);
    66.         point.SetActive (true);
    67.     }
    68.  
    69.     void StartSpawnProcess (float respawnTime) {
    70.         messageWindow.gameObject.SetActive (true);
    71.         connectionText.gameObject.SetActive (false);
    72.         sceneCamera.enabled = true;
    73.         StartCoroutine ("SpawnPlayer", respawnTime);
    74.     }
    75.  
    76.     IEnumerator SpawnPlayer(float respawnTime)
    77.     {
    78.         yield return new WaitForSeconds(respawnTime);
    79.  
    80.         int index = Random.Range (0, spawnPoints.Length);
    81.         player = PhotonNetwork.Instantiate ("Player", spawnPoints [index].position, spawnPoints [index].rotation, 0);
    82.         sceneCamera.enabled = false;
    83.  
    84.         player.GetComponent <Syncing>().SendNetworkMessage += AddMessage;
    85.  
    86.         AddMessage ("Spawned Player: " + PhotonNetwork.player.name);
    87.     }
    88.  
    89.     void AddMessage(string message){
    90.         photonView.RPC ("AddMessage_RPC", PhotonTargets.All, message);
    91.     }
    92.  
    93.     [PunRPC]
    94.     void AddMessage_RPC(string message){
    95.         messages.Enqueue (message);
    96.         if(messages.Count > messageCount){
    97.             messages.Dequeue ();
    98.         }
    99.  
    100.         messageWindow.text = "";
    101.         foreach(string m in messages)
    102.             messageWindow.text += m + "\n";
    103.     }
    104. }
    105.  
    Sync
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Syncing : Photon.MonoBehaviour {
    6.  
    7.     Vector3 position;
    8.     Quaternion rotation;
    9.     float smoothing = 10f;
    10.     public GameObject realGun;
    11.     public GameObject fake;
    12.     public float health = 5;
    13.  
    14.     public delegate void Respawn(float time);
    15.     public event Respawn RespawnMe;
    16.     public delegate void SendMessage(string MessageOverlay);
    17.     public event SendMessage SendNetworkMessage;
    18.  
    19.     void Start () {
    20.         if(photonView.isMine)
    21.         {
    22.             GetComponent <Rigidbody>().useGravity = true;
    23.             GetComponent <MouseLook>().enabled = true;
    24.             GetComponent <Movement>().enabled = true;
    25.             GetComponent <Shooting>().enabled = true;
    26.             GetComponentInChildren <Camera>().enabled = true;
    27.             realGun.SetActive (true);
    28.         }
    29.         else{
    30.             StartCoroutine("UpdateData");
    31.             fake.SetActive (true);
    32.         }
    33.     }
    34.  
    35.     IEnumerator UpdateData()
    36.     {
    37.         while(true)
    38.         {
    39.             transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
    40.             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
    41.             yield return null;
    42.         }
    43.     }
    44.  
    45.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    46.     {
    47.         if(stream.isWriting)
    48.         {
    49.             stream.SendNext(transform.position);
    50.             stream.SendNext(transform.rotation);
    51.             stream.SendNext (health);
    52.         }
    53.         else
    54.         {
    55.             position = (Vector3)stream.ReceiveNext();
    56.             rotation = (Quaternion)stream.ReceiveNext();
    57.             health = (float)stream.ReceiveNext ();
    58.         }
    59.     }
    60.  
    61.     [PunRPC]
    62.     public void Damage(float dmg, string enemyName){
    63.         health -= dmg;
    64.         if(health <= 0 && photonView.isMine)
    65.         {
    66.  
    67.             if (SendNetworkMessage != null) {
    68.                 SendNetworkMessage (PhotonNetwork.player.name + " was killed by " + enemyName);
    69.                 Debug.Log ("Got killed");
    70.             }
    71.  
    72.             if(RespawnMe != null)
    73.                 RespawnMe(3f);
    74.             PhotonNetwork.Destroy (gameObject);
    75.         }
    76.     }
    77. }
    78.  
    Shooting
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooting : Photon.MonoBehaviour {
    5.  
    6.     public GameObject point;
    7.     public GameObject pointfake;
    8.     public LineRenderer line;
    9.     public float damage = 1;
    10.     public float range = 100;
    11.  
    12.     public float fireRate = 1.0f;
    13.     private float lastShot = 0.0f;
    14.  
    15.     private Vector3 hitPub;
    16.  
    17.     void Update () {
    18.         if(Input.GetButton ("Fire1")){
    19.             Fire ();
    20.         }
    21.        
    22.     }
    23.     void Fire(){
    24.         if (Time.time > fireRate + lastShot) {
    25.             Debug.Log ("Fire");
    26.  
    27.             RaycastHit hit;
    28.             Camera fpsCam = GetComponentInChildren <Camera> ();
    29.             Ray ray = fpsCam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
    30.  
    31.             if (Physics.Raycast (ray, out hit, range)) {
    32.                 hitPub = hit.point;
    33.                 if (hit.transform.GetComponent <Shooting>() != null) {
    34.                     Debug.Log ("Hit");
    35.                     hit.transform.GetComponent<PhotonView> ().RPC ("Damage", PhotonTargets.AllBuffered, damage, PhotonNetwork.player.name);
    36.                 }
    37.                 /*if (photonView.isMine) {
    38.                     line.SetPosition (0, point.transform.position);
    39.                     line.SetPosition (1, hit.point);
    40.                     line.SetWidth (0.10f, 0.10f);
    41.                 } else {
    42.                     line.SetPosition (0, pointfake.transform.position);
    43.                     line.SetPosition (1, hit.point);
    44.                     line.SetWidth (0.10f, 0.10f);
    45.                 }*/
    46.                 GetComponent <PhotonView> ().RPC ("AddLine", PhotonTargets.AllBuffered, null);
    47.  
    48.                 Debug.Log (hit.transform.name);
    49.  
    50.             }
    51.  
    52.             lastShot = Time.time;
    53.         }
    54.     }
    55.  
    56.     [PunRPC]
    57.     void AddLine(){
    58.         line.SetPosition (0, point.transform.position);
    59.         line.SetPosition (1, hitPub);
    60.         line.SetWidth (0.10f, 0.10f);
    61.     }
    62.  
    63. }
    64.  
     
    Last edited: Dec 31, 2016
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Whoever created this tutorial, it's a catastrophe....
     
  3. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    it was unity... but i modified some of them my self
     
  4. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
  5. KarlGG

    KarlGG

    Joined:
    Nov 4, 2014
    Posts:
    34
    The error says what you need, your NetworkManager MonoBehaviour is in fact missing the OnPhotonSerializeView() it's complaining about. Your Sync class has one, but that's not where the error is pointing.
     
    Moddwyn likes this.