This code is attached to player prefab and creates 2 objects, in the same spot, which are both visible for all players.
One player can move one of these objects, and can't control the other, and the situation is the opposite for the second player.
How to make it so there is only one instantiated object, which can be controlled (moved around with another script that I have) by all players?
using UnityEngine;
using System.Collections;
public class SpawnGameObjectsNetwork : Photon.MonoBehaviour
{
public float secondsBetweenSpawning = 0.1f;
public float xMinRange = -25.0f;
public float xMaxRange = 25.0f;
public float yMinRange = 8.0f;
public float yMaxRange = 25.0f;
public float zMinRange = -25.0f;
public float zMaxRange = 25.0f;
public float zMinRange2 = 81.0f;
public float zMaxRange2 = 85.0f;
public string spawnObjects;
Vector3 spawnPosition;
Vector3 spawnPosition2;
public float nextSpawnTime;
void Start ()
{
nextSpawnTime = Time.time+secondsBetweenSpawning;
}
void Update()
{
if ( PhotonNetwork.isMasterClient )
{
if ( Time.time > nextSpawnTime )
{
Vector3 spawnPosition;
Vector3 spawnPosition2;
spawnPosition.x = Random.Range (xMinRange, xMaxRange);
spawnPosition.y = Random.Range (yMinRange, yMaxRange);
spawnPosition.z = Random.Range (zMinRange, zMaxRange);
spawnPosition2.x = Random.Range (xMinRange, xMaxRange);
spawnPosition2.y = Random.Range (yMinRange, yMaxRange);
spawnPosition2.z = Random.Range (zMinRange2, zMaxRange2);
GetComponent<PhotonView>().RPC( "MakeThingToSpawn", PhotonTargets.All, spawnPosition, spawnPosition2 );
nextSpawnTime = Time.time + secondsBetweenSpawning;
}
}
}
[PunRPC]
void MakeThingToSpawn( Vector3 spawnPosition, Vector3 spawnPosition2 )
{
GameObject spawnedObject = PhotonNetwork.Instantiate(spawnObjects, spawnPosition, transform.rotation, 0) as GameObject;
GameObject spawnedObject2 = PhotonNetwork.Instantiate(spawnObjects, spawnPosition2, transform.rotation, 0) as GameObject;
}
}
In your case, you need to instantiate a SceneObject using PhotonNetwork.InstantiateSceneObject(), and then transferOwnership on a particular Player when required.
There is a demo available in PUN package called "DemoChangeOwner". Check it out.
also read this thread too, it's informative: http://forum.photonengine.com/discussion/1844/photonnetwork-instantiate
for example, this is the job of the MasterClient to do this, so not all Clients are entitled.