1

I'm making a multiplayer system. I have a strange situation about Start(). When the first player joins there is 2 logs "player joined". So, when second player joins, their model instantiates twice (there is PhotonNetwork.Instantiate in Start()). I guess Start() is being called 2 times somehow?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
public class MPRoomServer : MonoBehaviour
{
    public GameObject rocket;
    public TMP_Text title;
    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.Instantiate(rocket.name, new Vector3(20.4928417f,4.3900001f,-10.116004f), Quaternion.identity);
        title.text = "Room " + PhotonNetwork.CurrentRoom.Name;
        Debug.Log("New player joined");
    }

    // Update is called once per frame
    void Update()
    {
    
    }
}
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • You probably have two copies of this script in your scene. In the scene window's hierarchy search bar, type "t:MPRoomServer" and it'll show you all the objects that have this script on them. If it shows a single object, you might have two copies of the script on a single object. – Foggzie Aug 06 '22 at 20:01
  • no, i have only one script on scene –  Aug 06 '22 at 20:18
  • Then you're instantiating another object with this script on it. `Start()` is only ever called once per-instance. – Foggzie Aug 06 '22 at 20:22

1 Answers1

0

OnJoinedRoom() works even when you create a room. So, my scene was loaded 2 times, and the script was used 2 times.

FalcoGer
  • 2,278
  • 1
  • 12
  • 34