Sync issue

Options
I have problems with synchronization, namely from readings, writes :

InvalidCastException: Specified cast is not valid.
GameController.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets / Script / GameController.cs: 44)

Here is the script itself. ↓↓↓↓ I will be grateful for the help if you need details write

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


public class GameController : MonoBehaviour, IPunObservable
{
private PhotonView photonView;
private SpriteRenderer psr;

[SerializeField]
public bool isRed;



public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
Debug.Log("Is Writing");
stream.SendNext(isRed);

#region rolesPlayerWriting
stream.SendNext(countroles1);
stream.SendNext(countroles2);
stream.SendNext(countroles3);
stream.SendNext(countroles4);

stream.SendNext(roles1full);
stream.SendNext(roles2full);
stream.SendNext(roles3full);
stream.SendNext(roles4full);

#endregion

}
else
{
Debug.Log("Is Reading");
isRed = (bool)stream.ReceiveNext();
#region rolesPlayerReading
roles1full = (bool)stream.ReceiveNext();
roles2full = (bool)stream.ReceiveNext();
roles3full = (bool)stream.ReceiveNext();
roles4full = (bool)stream.ReceiveNext();

countroles1 = (int)stream.ReceiveNext();
countroles2 = (int)stream.ReceiveNext();
countroles3 = (int)stream.ReceiveNext();
countroles4 = (int)stream.ReceiveNext();

#endregion
}
}

public void Awake()
{
roles = Random.Range(1, 4);

}

#region GivesRoles


public Text RolesText;
public int roles = 0;
public int countroles1;
public int countroles2;
public int countroles3;
public int countroles4;

public bool roles1full;
public bool roles2full;
public bool roles3full;
public bool roles4full;


public void GiveRolesPlayer() {
Debug.Log("Start Give Roles");
if (roles1full == false)
{
countroles1+= 1;
RolesText.text = "Вася Пупок";
roles = 1;
}
else
{
if (roles2full == false)
{
RolesText.text = "Зубенко Мехаил";
roles = 2;
roles2full = true;
}
else
{
if (roles3full == false)
{
RolesText.text = "Мусорелла";
roles = 3;
roles3full = true;
}
else
{
if (roles4full ==false)
{
RolesText.text = "Пилюлькин";
roles4full = true;
roles = 3;
}
else
{
Debug.Log("None Roles");
}
}

}
}

}

IEnumerator RolesCorutine()
{
yield return new WaitForSeconds(2);
Debug.Log("Start Gives Role Corutine");
GiveRolesPlayer();
}



#endregion

public void Start()
{
StartCoroutine(RolesCorutine());
photonView = GetComponent<PhotonView>();
Debug.Log("Start");
psr = GetComponent<SpriteRenderer>();
if (countroles1 >= 2)
{
roles1full = true;
}

}
public void Update()
{
if (!photonView.IsMine) return;
{

if (Input.GetKey(KeyCode.RightArrow)) transform.Translate(-Time.deltaTime * -5, 0, 0);
if (Input.GetKey(KeyCode.LeftArrow)) transform.Translate(-Time.deltaTime * 5, 0, 0);

if (Input.GetKey(KeyCode.Space))
{
isRed = true;
}
else
{
isRed = false;
}
}

if (isRed)
{
psr.color = Color.red;
}
else
{
psr.color = Color.white;
}
}
}

Comments

  • S_Oliver
    S_Oliver ✭✭✭
    Options

    Receiving should be in the same order as sending. You mixed it.

  • dimonvyt
    Options
    Thank you, I have 1 more question why my bool variables work only for 1 player and nothing happens for all.
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Sorry but thats not enough information to give you an answer.
  • dimonvyt
    dimonvyt
    edited April 2020
    Options
    There is a variable isRed, and when you press the Space button, the object should turn red on all clients connected to the server, but it works only on 1 client, and not on the rest
    E7qIkVZ.png
    ckSCG6M.png
    JUf25oT.png


  • S_Oliver
    S_Oliver ✭✭✭
    edited April 2020
    Options
    Check you Update method again!
    if(!photonView.IsMine)return;
    {
    }
    
    stops all code execution if isMine returns false
  • dimonvyt
    Options
    Sorry, but could you describe in more detail what needs to be done?
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Structure your update method like this
    if (photonView.IsMine)
    {
    	//your code
    	//your code
    }
    
    if (isRed)
    {
    	//your code
    }
    else
    {
    	//your code
    }
    

    In your code this line is the "problem".
    if (!photonView.IsMine) return;
    
    change it to
    if (photonView.IsMine)
    
  • dimonvyt
    Options

    Thank you very much!