Transfership set to scene but gameobject still gets destroyed when master client leaves

Hi, I'm trying to manually instantiate a scene object and sync it's transform using Photon Transform View, everything works great and it's syncing , but the only problem I'm having is even after setting the gameobjects ownership to 0 (scene) when I disconnect the master client who instantiated it , it gets destroyed. Here's my code:

IEnumerator spawm(float starttime)
{
...
...
...
PhotonView photonView = this.GetComponent ();
int id = PhotonNetwork.AllocateViewID ();
if (PhotonNetwork.isMasterClient)
photonView.RPC ("spawnNetwork", PhotonTargets.All, a, directionPos, pos, euler, id);
...
...
...
}

[PunRPC]
void spawnNetwork(int a, int directionPos, Vector2 pos, Vector3 euler, int id){

_tr = Instantiate (_pre [a]).transform; //instantiate the fish
_tr.position = pos;
_tr.eulerAngles = euler;
if (PhotonNetwork.isMasterClient) {
_tr.GetComponent ().enabled = true;
_tr.GetComponent ().enabled = true;
}
_tr.GetComponent ().viewID = id;
_tr.GetComponent ().TransferOwnership (0);

}
I understand I can use Photon.InstantiateSceneObject, but the game I'm working on is a single player game template that I bought and that I'm trying to convert to multiplayer and moving so much enemy prefabs to the Resources folder ends up breaking scripts. As a noob it took me two full days to figure out how to sync them and I've learned alot and how powerful Photon can be if used the right way and I'm loving it :).. so just wanted to see if anyone had any advice on how to keep those instantiated gameobjects in the game even when they're owned by the scene. Thanks!

Comments

  • yaezah
    yaezah
    edited May 2018
    Not sure why the code in angle brackets above was removed ,but here it is with spaces in between the brackets in the code:

    PhotonView photonView = this.GetComponent< PhotonView > ();
    int id = PhotonNetwork.AllocateViewID ();
    if (PhotonNetwork.isMasterClient)
    photonView.RPC ("spawnNetwork", PhotonTargets.All, a, directionPos, pos, euler, id);

    [PunRPC]
    void spawnNetwork(int a, int directionPos, Vector2 pos, Vector3 euler, int id){
    _tr = Instantiate (_pre [a]).transform; //instantiate the fish
    _tr.position = pos;
    _tr.eulerAngles = euler;
    if (PhotonNetwork.isMasterClient) {
    _tr.GetComponent< FishSignFreeControl > ().enabled = true;
    _tr.GetComponent< Swim > ().enabled = true;
    }
    _tr.GetComponent< PhotonView > ().viewID = id;
    _tr.GetComponent< PhotonView > ().TransferOwnership (0);

    }
  • I think I got it to work. For those who are running into a similar issue, instead of allocating a normal view ID , try allocating a scene view id like this:

    int id = PhotonNetwork.AllocateSceneViewID ();

    this makes it so that even if the person who instantiated it leaves, the gameobjects don't get destroyed and get their ownership passed to the next master client (just like PhotonInstantiateSceneObject. Also since it's a scene object now I just deleted the transferownership line and it still worked and transfered ownership to the next master client automatically.