Duplicate IDs and key conflicts

Options
I have the following code:
protected void EnableDamage()
    {
        if (hasSpawnedDamage) return;
        hasSpawnedDamage = true;

        gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;

        var spellTransform = transform;
        
        spawnedDmgDestroy = Instantiate(dmgDestroy, spellTransform.position, spellTransform.rotation);
        PhotonView spawnedPhotonView = spawnedDmgDestroy.GetComponent<PhotonView>();

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

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

            SendOptions sendOptions = new SendOptions
            {
                Reliability = true
            };

            // Destroy(spawnedPhotonView); //I want to recreate it at the same time as everybody else!
            PhotonNetwork.RaiseEvent(2, data, raiseEventOptions, sendOptions);
        }
        else
        {
            Debug.LogError("Failed to allocate a ViewId.");

            Destroy(spawnedDmgDestroy);
        }
    }
    
    public void OnEvent(EventData photonEvent)
    {
        if (photonEvent.Code == 2)
        {
            object[] data = (object[]) photonEvent.CustomData;

            if (!spawnedDmgDestroy)
            {
                spawnedDmgDestroy = Instantiate(dmgDestroy, (Vector3) data[0], (Quaternion) data[1]);
                PhotonView photonView = spawnedDmgDestroy.GetComponent<PhotonView>();
                photonView.ViewID = (int) data[2];
            }

            spawnedDmgDestroy.GetComponent<Spells.Dmg_Destroy>().AwakeInstantiate(gameObject);
        }
    }
I get the following errors when trying to do custom instantiation.

I know these are the lines that cause the error, as when I remove them the errors go away. My question is how do I stop this? I am using custom instantiation as the document suggest.

In this case, 5 items are being spawned at once by the same client.

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited September 2019
    Options
    Hi @RacerDelux,

    In this case, 5 items are being spawned at once by the same client.
    Could you share the code snippet that calls EnableDamage method and does the instantiation 5 times? Also, could you share the stack trace for the "ArgumentException" error?
  • I figured it out, it was a bug with OnEvent. First off I was not checking to make sure the event was only called by the photonView that called it. Second I was passing in the wrong id. I was doing photonView.ViewID when I should have been doing SpawnedDmgDestroy.ViewID.

    In addition, in OnEvent I should not use photonView as the variable name, as this conflicts with the private member photonView.

    All fixed now though.