Search Unity

Third Party Photon Pun 2 assigning players' parent on each of their clients, but doesn't show together.

Discussion in 'Multiplayer' started by superkasper, May 24, 2023.

  1. superkasper

    superkasper

    Joined:
    Apr 29, 2020
    Posts:
    1
    Hello, I have a 2-player multiplayer game where the players first select their character in the selection menu:
    upload_2023-5-24_17-21-47.png

    When they press start the game loads another level, but there is a DontDestroyOnLoad object called StatObject.
    upload_2023-5-24_17-22-50.png
    This Object contains the script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using Photon.Pun;
    6.  
    7. public class StatObjectScript : MonoBehaviour
    8. {
    9.     public GameObject P1;
    10.     public GameObject P2;
    11.  
    12.     bool spawned;
    13.  
    14.     bool found;
    15.  
    16.     [SerializeField]
    17.     GameObject P1Spawn;
    18.  
    19.     [SerializeField]
    20.     GameObject P2Spawn;
    21.  
    22.     public string P1GameObject;
    23.     public string P2GameObject;
    24.  
    25.     GameObject waitForPlayers;
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.         DontDestroyOnLoad(gameObject);
    31.     }
    32.  
    33.     void Update()
    34.     {
    35.         if(GameObject.Find("WaitForPlayers"))
    36.         {
    37.             return;
    38.         }
    39.  
    40.         if (SceneManager.GetActiveScene().name == "FightArena" && spawned == false)
    41.         {
    42.             spawned = true;
    43.             P1Spawn = GameObject.Find("P1Spawn");
    44.             P2Spawn = GameObject.Find("P2Spawn");
    45.             if (PhotonNetwork.LocalPlayer.ActorNumber == 1)
    46.             {
    47.                 GameObject spawn1 = PhotonNetwork.Instantiate(P1.name, P1Spawn.transform.position, P1Spawn.transform.rotation);
    48.  
    49.                 spawn1.transform.parent = P1Spawn.transform;
    50.  
    51.                 P1GameObject = spawn1.name;
    52.             }
    53.  
    54.             if (PhotonNetwork.LocalPlayer.ActorNumber == 2)
    55.             {
    56.                 GameObject spawn2 = PhotonNetwork.Instantiate(P2.name, P2Spawn.transform.position, P2Spawn.transform.rotation);
    57.  
    58.                 spawn2.transform.parent = P2Spawn.transform;
    59.  
    60.                 P2GameObject = spawn2.name;
    61.             }
    62.         }
    63.  
    64.  
    65.         if(spawned)
    66.         {
    67.             if(P1Spawn.GetComponentInChildren<PlayerScript>().dead)
    68.             {
    69.                 StartCoroutine(WaitForTime());
    70.             }
    71.  
    72.             if (P2Spawn.GetComponentInChildren<PlayerScript>().dead)
    73.             {
    74.                 StartCoroutine(WaitForTime());
    75.             }
    76.         }
    77.     }
    78.  
    79.     IEnumerator WaitForTime()
    80.     {
    81.         yield return new WaitForSeconds(4f);
    82.         PhotonNetwork.LoadLevel("StartMenu");
    83.     }
    84. }
    It's supposed to assign the players' parent to these gameobjects:
    upload_2023-5-24_17-24-20.png
    It does that but only on their own client, so P1 is assigned to P1Spawn on his client but P2 is not. How can I fix?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Code (CSharp):
    1. GameObject spawn2 = PhotonNetwork.Instantiate(...)
    In this case, spawn2 is just a local object reference. The code you run on it won't be running on the remote machines and setting the parent is not synchronized automatically either. This means your parenting happens only on the client which is instantiating.
    It might be better to reparent the client in a script that implements OnPhotonInstantiate. That would at least run on all instances (on the client that created the object initially but also all proxies).

    See "Instantiation".