Photon instantiating crates happens twice?

Hi,

This is my instantiation code for upgrade crates in each scene in my multiplayer game:

    IEnumerator CrateDrop()
    {
        while (crateCount < 200)
        {
            xPos = Random.Range(50, 390);
            zPos = Random.Range(50, 390);
            //PhotonNetwork.Instantiate(crate.name, new Vector3(xPos, 26, zPos), Quaternion.identity);
            PhotonNetwork.Instantiate(crate.name, new Vector3(xPos, 26, zPos), Quaternion.identity, 0);

            yield return new WaitForSeconds(15);
            crateCount += 1;


        }

    }

They spawn at random locations within the given range but If there is more then one player in the scene, the instantiation happens twice and I don’t know why this is happening.
I just need them to spawn one time, does anybody know why this happens?

You need to make sure the script only runs on the owning player object, take a look at the documentation for photonView.IsMine.

If you add a check for it to this script it should fix your problem, assuming this script is on the GameObject that has the PhotonView.

    IEnumerator CrateDrop()
    {
        if(!photonView.IsMine) yield break;

        while (crateCount < 200)
        {
            xPos = Random.Range(50, 390);
            zPos = Random.Range(50, 390);
            //PhotonNetwork.Instantiate(crate.name, new Vector3(xPos, 26, zPos), Quaternion.identity);
            PhotonNetwork.Instantiate(crate.name, new Vector3(xPos, 26, zPos), Quaternion.identity, 0);
            yield return new WaitForSeconds(15);
            crateCount += 1;
        }
    }
2 Likes