Instantiating scene object when not being a master

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Instantiating scene object when not being a master

bdar
2020-10-02 12:06:58

Hi, I have a problem with the player's game objects being deleted when the player leaves a room. While this is ok for the player's avatar, I would like the rest of the instantiated game objects to remain in the room for the rest of the players. I recently learned about PhotonNetwork.InstantiateSceneObject but that will only work if the player is the master, and I need that to work for all the players. I cannot follow the advice from this forum, because I need to work with the instantiated game object immediately and since there is no way of knowing when the instantiation of the object took place and RPC does not return any values, I will only end up with a bunch of null object exceptions. I also tried roomOptions.CleanupCacheOnLeave = false. I can deal with the fact, that I have to manually clean my avatars but it is giving me this error when trying to leave one room and join another "PhotonView ID duplicate found: 1001. New: View 1001 on Avatar(Clone) old: View 1001 on Avatar(Clone) . Maybe one wasn't destroyed on scene load?! Check for 'DontDestroyOnLoad'. Destroying old entry, adding new.
UnityEngine.Debug:LogError(Object)". Also, all the objects remain when I change a room and that is not acceptable. I only need the objects to remain within a room until the last player leaves. How do I achieve that? A possible accepted solution would be to somehow transfer the ownership from the player to the room after the instantiation process is finished but I didn't come across a way to do that in PUN 2.

Is there any advice or help regarding my problem? Thanks a lot in advance.

Comments

emotitron
2020-10-03 13:36:52

You might be needing to look into manual instantiation. It is a bit more complicated, but you have more control over the process.

https://doc.photonengine.com/en-us/pun/current/gameplay/instantiation#manual_instantiation

One issue with trying to instantly instantiate a room object on a non-master client, is that there can be some uncertainty about the viewID. For example, if two players try to spawn a new object like this at the same time, they might both try to assign it the next available room view Id. Say 0002 ... and now you have a collision to resolve.

Players can instantiate their own because they are the authority on creating viewIDs for their own objects. Room objects rely on the master for that assignment. Scene/Room objects rely on the master as that authority.

FredyGonzale
2022-01-28 01:12:48

Hello, you can now solve your problem by wanting to create a scene object without being the master client.

What you need to do is create an RPC and send it to the master client, and the master client will create the prefab you want

FredyGonzale
2022-01-28 01:47:28

Example

//Components privates

private PhotonView PV;

private void Awake()

{

PV = GetComponent();

}

private void Start()

{

PV.RPC("RPC_CreateObject_MasterClient", RpcTarget.MasterClient, this.transform.position);

}

private void CreateObjectInScene(Vector3 position )

{

GameObject obj = PhotonNetwork.InstantiateRoomObject(Path.Combine("PhotonPrefabs", "ObjectPhoton"), position , Quaternion.identity);

}

[PunRPC]

void RPC_CreateObject_MasterClient(Vector3 _position)

{

CreateObjectInScene(_position);

}

rogerpi
2022-05-29 13:39:23

@FredyGonzaleMAN THX VERY MUCH

Back to top