Photon Manual Instantiation PhotonView.Owner is NULL

Hello, I'm spawning player with this code. When I check PhotonView.Owner it gives null. Thank you. Is it bug or normal? What can I do for it? Update: Also PhotonView Owner value in inspector is" Scene" and PhotonView.IsMine is true for both player which is wrong..
public void SpawnPlayer()
{
    GameObject player = Instantiate(PlayerPrefab);
    PhotonView photonView = player.GetComponent<PhotonView>();

    if (PhotonNetwork.AllocateViewID(photonView))
    {
        object[] data = new object[]
        {
            player.transform.position, player.transform.rotation, photonView.ViewID
        };

        RaiseEventOptions raiseEventOptions = new RaiseEventOptions
        {
            Receivers = ReceiverGroup.Others,
            CachingOption = EventCaching.AddToRoomCache
        };

        SendOptions sendOptions = new SendOptions
        {
            Reliability = true
        };

        PhotonNetwork.RaiseEvent(CustomManualInstantiationEventCode, data, raiseEventOptions, sendOptions);
    }
    else
    {
        Debug.LogError("Failed to allocate a ViewId.");

        Destroy(player);
    }

private void OnEnable()
    {
        PhotonNetwork.AddCallbackTarget(this);
    }

    private void OnDisable()
    {
        PhotonNetwork.RemoveCallbackTarget(this);
    }

    public void OnEvent(EventData photonEvent)
    {
        if (photonEvent.Code == CustomManualInstantiationEventCode)
        {
            object[] data = (object[]) photonEvent.CustomData;

            GameObject car = (GameObject) Instantiate(Cars[(int)data[4]], Vector3.zero, Quaternion.identity);
            
            car.transform.GetChild(0).position = (Vector3) data[0];
            car.transform.GetChild(0).rotation = (Quaternion) data[1];

            PhotonView photonView = car.GetComponent<PhotonView>();
            photonView.ViewID = (int) data[2];
            car.transform.GetChild(0).GetComponent<PhotonView>().ViewID = (int) data[3];
        }
    }
        
}

Comments

  • Update: Also PhotonView Owner value in inspector is" Scene".
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @MaverickTC,

    Thank you for choosing Photon!

    when do you check PhotonView.Owner?
    in Awake, or after?

    I just tested manual instantiation as we show it in the example on the documentation and it works as expected. I could not reproduce.

    the code is not correct in OnEvent: the array you send has 3 elements and you try to get a fourth element:

    what you send:
    object[] data = new object[]
            {
                player.transform.position, player.transform.rotation, photonView.ViewID
            };
    

    you try to get an element that does not exist (should throw exception)
    car.transform.GetChild(0).GetComponent<PhotonView>().ViewID = (int) data[3];
    

    another note (just an observation):

    instantiated prefabs are different from sender and receiver:
    GameObject player = Instantiate(PlayerPrefab);
    

    vs.
    GameObject car = (GameObject) Instantiate(Cars[(int)data[4]], Vector3.zero, Quaternion.identity);
    

    So I'm thinking maybe you have shared the code from the example but your actual code is different.
  • MaverickTC
    edited August 2020
    Thank you very much for reply @JohnTube,
    I think I did something wrong while copying my old code. I edited code and my problem is exist.
    This is my new whole code.
    byte CustomManualInstantiationEventCode = 159;
    
    void Start()
        {
            Resources.UnloadUnusedAssets();
            StartCoroutine(PlayerConnected());
        }
    
    IEnumerator PlayerConnected()
        {
            createPlayer = true;
            
            Application.runInBackground = true;
            GameObject car;
            if (MultiplayerOrNot.MultiplayerOr)
            {
                PhotonNetwork.KeepAliveInBackground = 40;
    
                car = Instantiate(Cars[PlayerPrefs.GetInt("SelectedCar")],Vector3.zero,Quaternion.identity);
                PhotonView photonView = car.GetComponent<PhotonView>();
    
                Quaternion quaternion = new Quaternion(SpawnPosition.rotation.x, SpawnPosition.rotation.y, SpawnPosition.rotation.z, 1);
                car.transform.GetChild(0).rotation = quaternion;
                car.transform.GetChild(0).position = SpawnPosition.position;
    
                if (PhotonNetwork.AllocateViewID(photonView))
                {
                    if (PhotonNetwork.AllocateViewID(car.transform.GetChild(0).GetComponent<PhotonView>()))
                    {
                        object[] data = new object[]
                        {
                            car.transform.GetChild(0).position, car.transform.GetChild(0).rotation, photonView.ViewID, car.transform.GetChild(0).GetComponent<PhotonView>().ViewID, PlayerPrefs.GetInt("SelectedCar")
                        };
    
                        RaiseEventOptions raiseEventOptions = new RaiseEventOptions
                        {   
                            Receivers = ReceiverGroup.Others,
                            CachingOption = EventCaching.AddToRoomCache
                        };
    
                        SendOptions sendOptions = new SendOptions
                        {
                            Reliability = true
                        };
    
                        PhotonNetwork.RaiseEvent(CustomManualInstantiationEventCode, data, raiseEventOptions, sendOptions);
    
                        ch = car.GetComponent<ChatManager>();
                        pi = car.GetComponent<PlayerInfo>();
                        myCar = car;
    
                        car.transform.GetChild(0).GetComponent<CarDriver>().SpawnPosition = SpawnPosition;
                        car.transform.GetChild(0).GetComponent<CarDriver>().PortPosition = PortPosition;
                        car.transform.GetChild(0).GetComponent<CarDriver>().RaceTrackPosition = RaceTrackPosition;
                        car.transform.GetChild(0).GetComponent<CarDriver>().PetrolPosition = PetrolPosition;
                        if (ch == null)
                            ch = car.transform.GetChild(0).GetComponent<ChatManager>();
                        if (pi == null)
                            pi = car.transform.GetChild(0).GetComponent<PlayerInfo>();    
                        CheckNicknameDuplicate(car);
                    }
                    else
                    {
                        Debug.LogError("Failed to allocate a ViewId.");
                        Destroy(car);
                    }
                } else {
                    Debug.LogError("Failed to allocate a ViewId.");
                    Destroy(car);
                }
            } else {
    
                car = Instantiate(Cars[PlayerPrefs.GetInt("SelectedCar")], SpawnPosition.position, Quaternion.identity);
                for(int i = 0; i < car.transform.childCount; i++)
                {
                    GameObject child = car.transform.GetChild(i).gameObject;
    
                    Quaternion quaternion = new Quaternion(SpawnPosition.rotation.x, SpawnPosition.rotation.y, SpawnPosition.rotation.z, 1);
                    child.transform.GetChild(0).rotation = quaternion;
                    child.transform.GetChild(0).GetComponent<CarDriver>().SpawnPosition = SpawnPosition;
                    child.transform.GetChild(0).GetComponent<CarDriver>().PortPosition = PortPosition;
                    child.transform.GetChild(0).GetComponent<CarDriver>().RaceTrackPosition = RaceTrackPosition;
                    child.transform.GetChild(0).GetComponent<CarDriver>().PetrolPosition = PetrolPosition;
    
                    if (myCar == null)
                        myCar = child;
                    if (ch == null)
                        ch = myCar.GetComponent<ChatManager>();
                    if(pi == null){
                        pi = myCar.GetComponent<PlayerInfo>();
                    }   
                }
            }
    
            if(PlayerPrefs.GetInt("RemoveAds")!=1){
                if(Random.Range(0,2) == 0){
                    #if (UNITY_ANDROID || (UNITY_IPHONE ))
                    ReklamScript.InsterstitialGoster();
                    #endif
                }
            }
            yield return null;
        }
            
        private void OnEnable()
        {
            PhotonNetwork.AddCallbackTarget(this);
        }   
    
        private void OnDisable()
        {
            PhotonNetwork.RemoveCallbackTarget(this);
        }   
    
        public void OnEvent(EventData photonEvent)
        {
            if (photonEvent.Code == CustomManualInstantiationEventCode)
            {
                object[] data = (object[]) photonEvent.CustomData;
    
                GameObject car = (GameObject) Instantiate(Cars[(int)data[4]], Vector3.zero, Quaternion.identity);
                
                car.transform.GetChild(0).position = (Vector3) data[0];
                car.transform.GetChild(0).rotation = (Quaternion) data[1];
    
                PhotonView photonView = car.GetComponent<PhotonView>();
                photonView.ViewID = (int) data[2];
                car.transform.GetChild(0).GetComponent<PhotonView>().ViewID = (int) data[3];
            }
        }
    
    My MultiplayerOrNot.MultiplayerOr variable is always true and OnEvent code working.
    I'm spawning Cars[x] gameObject with photon view and Cars[x] have child with photon view.

    Let me explain the situation.
    There are two players.(Player A and Player B)
    1- Player A creating game and Player B joining it.
    On Player A's device both PhotonView's for both player is at bottom
    https://ibb.co/60GHL5m
    https://ibb.co/dK6ZpJj
    2- Player B creating game and Player A joining it.
    On Player B's device both PhotonView's for both player is at bottom
    https://ibb.co/bX6TdLv
    https://ibb.co/MZnn5ZH

    Note: All Photos is Cars[x]'s photonView photos. They are same for childs.

    Also I will try to reproduce it at basic project.
    *** Edit: I tested with basic scene and Owner side of Photon View is not scene like my game.

    Thank you again.
  • I found something; I was using Smooth Sync and When I remove it, problem is resolving. But I don't know what is wrong with Smooth Sync.
  • Update: I removed Smooth Sync and other things but problem still exist :'(

  • Be sure you have the current store version of Pun2. There have been a series of optimizations made to IsMine and ownership, but to check to see if this is related we need you to be current version.

  • Yes, I'm using last version of it. I think I found problem at some scripts I'm checking Photon View IsMine at void Start() and script making Initialization after Start function so it's making problem and broke initialization progress.
    I added some delay script into start but Is there better way to do it?
    Thank you.