Photon Unity - Only receiving one value.

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
if(playerNumber != 1)
{
stream.SendNext(Input.GetAxisRaw(“Vertical”));
}
if(playerNumber == 1)
{
GameObject listOfPlayers = GameObject.FindGameObjectsWithTag(“Player”);

			foreach(GameObject player in listOfPlayers)
			{
				stream.SendNext((Vector2)player.transform.position);
			}
		}
	}
	else
	{
		vertAxis = (float)stream.ReceiveNext();

		if(playerNumber != 1)
		{
			GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");

			if(listOfPlayers.Length != 0)
			{
				foreach(GameObject player in listOfPlayers)
				{
					player.transform.position = (Vector2)stream.ReceiveNext();
				}
			}
		}

	}
}

If the code is like this then the values for player.transform.position won’t be received. But if I remove:

			vertAxis = (float)stream.ReceiveNext();

Then the values for player.transform.position will be received.

Can anyone explain what’s going on or come up with a fix? Thanks.

Okay awesome. I don’t know what script that you posted is attached to, but what I would do is, attach this script to every single player:

public class NetworkCharacter : Photon.MonoBehaviour 
{
	Vector3 realPosition;

	void Update () 
	{
		if (photonView.isMine) 
		{
			//do nothing
		} 
		else 
		{
			//lerping position
			transform.position = Vector3.Lerp (transform.position, realPosition, .15f); 
		}
	}
		
	void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting) 
		{
			stream.SendNext(transform.position);
                } 
                else 
                {
			realPosition = (Vector3)stream.ReceiveNext ();
		}
	}
}

If you attach this to every player, than what this will do is have each player keep track of each other’s positions. Dont use your other script anymore.

Let me know if you have questions.

-Will

Thanks for your help, Will.

I don’t quite see the problem with for statements because from my testing I haven’t found them causing any issues.

Anyway, thank you for the code you provided. However it doesn’t quite do what I want because I want to send all players input to one ‘host’ player to create a kind of authoritative server.

I have finally managed to find a solution though:

	void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (stream.isWriting)
		{
			if(photonView.isMine && playerNumber != 1)
			{
				stream.SendNext(Input.GetAxisRaw("Vertical"));
			}

			if(photonView.isMine && playerNumber == 1)
			{
				GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");

				foreach(GameObject player in listOfPlayers)
				{
					stream.SendNext(player.transform.position);
				}
			}
		}
		else
		{

			GameObject[] listOfPlayers = GameObject.FindGameObjectsWithTag("Player");

			for(int i=0; i < listOfPlayers.Length; i++)
			{
				if(listOfPlayers_.GetPhotonView().isMine && listOfPlayers*.GetComponent<Player>().playerNumber == 1)*_

* {*
* vertAxis = (float)stream.ReceiveNext();*
* }*
* }*

* for(int i=0; i < listOfPlayers.Length; i++)*
* {*
if(listOfPlayers_.GetPhotonView().isMine && listOfPlayers*.GetComponent().playerNumber != 1)//listOfPlayers.Length != 0)
{
foreach(GameObject player in listOfPlayers)
{
player.transform.position = (Vector3)stream.ReceiveNext();
}
}
}
}
}*_

I think your code clashes with the idea that each individual PhotonView is controlled by it’s owner. This makes sure each player can affect it’s own objects and then send the new position/state to the others.

Your code looks like you run the script on all clients but only have one client send updates for every player object. That can’t work. How do you get the other client’s input?

Take a look at the Marco Polo Tutorial. It shows how to send and receive values like you try to.

If that doesn’t apply to your case, you need to post what you want to achieve.