Using Photon in Unity cannot sync variables through RPC call

39 Views Asked by At

So i am working on a multiplayer game made in Unity using Photon,

i have a gameobject with a phtonView component and a script with a bool that i want to synchronize to all clients

but i have a problem, if i synchronize the bool with an RPC call than it would work fine but since not all clients are loaded in to the room when it is called, all clients that are late wont have the bool synchronized after finally joining,

i have 2 clients, one creates a room, at start() bool is set to true (using an RPC call) for the first client; another one joins, its bool is not set to true because it was not active during the RPC call of the first client,

i tried using photon [SyncVar] but it doesnt recognize it and i dont want to make constant RPC calls when only one is necesary,

what can i do to synchronize a bool variable through all clients with a single call even if not all clients are loaded in?

1

There are 1 best solutions below

1
Ryan On

What if you have a main script with a function that updates all clients--call said function when client joins.

I didn't test any of this and it's been like 10 years since I did anything in Unity lol -- also I think this would be bad with more clients also and if Unity RPC has an in built method of doing this it's def preferrable

e.g.

// Server.cs
using UnityEngine;
public class Server : MonoBehaviour {
    public ArrayList<Client>() clients;
    public void SyncBool(bool val) {
        foreach (Client c: clients) {
            c.setBool(val);
        }    
    }
}

// Client.cs    
using UnityEngine;
public class Client : MonoBehaviour {
    public Server server;
    bool syncMe;
    void SetBool(bool set) {
        syncMe = set;
    }
    void Start(){
        server.clients.add(this.GameObject()); //i don't know what this would be lol 
        server.SyncBool(True);
    }
}

you'd have to like drag the server script onto the prefab of the client i think too?