PhotonNetwork.Instantiate GameObject owner?

Hello Forum.

I have a problem with ownership of the network-instantiated gameobjects. This problem is based on the assumption, that the ownership belongs automatically to the PhotonPlayer who instantiates the gameobject. I've just started using Photon, and I'm not quite familiar with the exact operation of the methods and such.

The game is PvP, with max number of players in one room limited to 2.

What I want to happen:

Both clients create their own team members (5/team). Team members have PhotonView component in them, and the ownerID should be the same as the creator client's.

What actually happens:

The team creation is successful, but instead of 5 members of "player1" and 5 members of "player2", I have 10 members of "player1". And yet the other team-specific variables are all correct, only the PhotonView ownership is wrong.

Pieces of code used:

This is the public NetworkManager, which is in the scene. After the RPC-call, the PhotonView's attached to these Player templates are set correctly: with correct ID's and local player bool values.
void OnPhotonPlayerConnected(PhotonPlayer _otherPlayer){

		if (PhotonNetwork.room.playerCount == PhotonNetwork.room.maxPlayers) {
			
			Debug.Log ("Enough Players");
			photonView.RPC("SpawnPlayer", PhotonTargets.All, null);
			_battleInitMP.GetComponent<BattleInitMP>().CreateArena();
		
		}
		else {
			Debug.Log ("Not Enough Players, waiting...");
		}

	}

[RPC]
	void SpawnPlayer(){

		Debug.Log ("SpawnPlayer");
		GameObject _player = PhotonNetwork.Instantiate ("PlayerMP", Vector3.zero, Quaternion.identity, 0);
		_player.GetComponentInChildren<AudioListener> ().enabled = true;
		_player.GetComponentInChildren<Camera> ().enabled = true;
		_player.GetComponentInChildren<WorldCameraMP> ().enabled = true;
		EnablePublicScripts ();
}

This is the player team generator. This script can be found from the root of the network-instantiated Player template. This is where the problem occurs. Even though the UpdatePlayerInfo() works perfectly, setting the _teamName and _photonPlayerID correctly according to the PhotonPlayer information, for some reason the PhotonNetwork.Instantiate in the CreatePlayerTeam() sets the "CharacterMP" photonview ownership to the master, instead the owner of this script. The commented parts after the Network.Instantiate are the means I transfer the ownership correctly. But I don't understand this situation, is the PhotonNetwork.Instantiate working as it should be?
public class TeamManagerMP : Photon.MonoBehaviour {

	public string _teamName;
	public int _photonPlayerID;
	public int profileRound;

	void Start () {

		UpdatePlayerInfo ();

	}
	public void UpdatePlayerInfo(){

		_photonPlayerID = photonView.ownerId;
		_teamName = photonView.owner.name;
		profileRound = _photonPlayerID;
		CreatePlayerTeam ();

	}

	public void CreatePlayerTeam(){


						Debug.Log ("CreatePlayerTeam, owner is " + photonView.owner.name);

						PlayerProfile profile = new PlayerProfile ();

						profile.SetName (_teamName);
						for (int i = 0; i < 5; i++) {

								GameObject prefabGameObject = PhotonNetwork.Instantiate ("CharacterMP", Vector3.zero, Quaternion.identity, 0) as GameObject;

								//if(prefabGameObject.GetComponent<PhotonView>().ownerId != _photonPlayerID)
									//prefabGameObject.GetComponent<PhotonView>().TransferOwnership(_photonPlayerID);


Question/Problem:

Is there a way to assign the PhotonNetwork.Instantiate ownership automatically to the owner of the script? Even though the commented parts fix the problem, I'd still like to know if there's a better way to do this. Am I doing something wrong?

Thank you for the replies.

Ile

Comments

  • I'm not so sure I fully understand the situation and cause.
    In general: PhotonNetwork.Instantiate() will instantiate a prefab and update the PhotonView on the new GO. The creator is the owner by default, too. You don't have to transfer the ownership.

    If you run TeamManagerMP as component on the "PlayerMP", your problem will be a logical one. PhotonNetwork.Instantiate will create the GO on all clients. So any script on those GOs will run everywhere. Basically, both client will run 2 instances of PlayerMP and TeamManagerMP.
    You only want the local PlayerMP to instantiate the team? Check photonView.isMine or run CreatePlayerTeam() from the SpawnPlayer() method. Each client will create 5 team member for itself.