Search Unity

Sync over the network the state of a gameobject

Discussion in 'Multiplayer' started by Squishy_Hrt, Apr 21, 2022.

  1. Squishy_Hrt

    Squishy_Hrt

    Joined:
    Dec 22, 2021
    Posts:
    1
    Hello everyone,

    I'm currently struggling about how can I synchronize the state of a game object over the network, using Photon. Pun.
    Here's my situation. I've got a game object ( a tombstone) and as a child of it, a light. On my tombstone, I've got a box collider checking if a player is nearby. And when a player's touching the box, the light turns on. But if I've got 2 players on the scene, only the one that triggers the event see it and not the other one.

    So I was thinking about using RPCs but is it the best solution. And if it is, how can I do it. Hers's my code:

    Code (CSharp):
    1. using Photon.Pun;
    2. using UnityEngine;
    3.  
    4. namespace EnvironmentScript
    5. {
    6.     public class TombEvent : MonoBehaviourPun
    7.     {
    8.         public Light spotLight;
    9.  
    10.         public void Start()
    11.         {
    12.             spotLight.gameObject.SetActive(false);
    13.         }
    14.        
    15.         private void OnTriggerEnter(Collider other)
    16.         {
    17.             if (base.photonView.IsMine && other.CompareTag("PlayerMulti"))
    18.                 OnLight();
    19.         }
    20.        
    21.         private void OnTriggerExit(Collider other)
    22.         {
    23.             OffLight();  
    24.         }
    25.            
    26.         private void OnLight()
    27.         {
    28.             spotLight.gameObject.SetActive(true);
    29.         }
    30.  
    31.         private void OffLight()
    32.         {
    33.             spotLight.gameObject.SetActive(false);
    34.         }
    35.     }
    36. }
    Thanks.
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    No RPC needed. If you want only the player activated the tombstone see the light just active it localy. The script above should work.