Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Unity Multiplayer Change variable on all clients from server

Discussion in 'Connected Games' started by kigm, Jun 27, 2016.

  1. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Hi! I need have to a variable on server that clients consults and in function of its value do something.

    I had been trying with a [SyncVar] but this is not what I want. Because I want that if one client connects to server, change this variable on the server and the rest of clients that connect later check this variable and work in function on that.
    And now if I use syncvar each client has his own variable, synced with the server yes, but it is his own variable, isn't shared between clients.

    What can I do?
     
  2. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
  3. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Really I think even more yesterday that doc is not so good... Because I read that this thing (Syncvar) updates a value from a server to all clients, and this is what I want. But doing tests I see that each client has his own variable, synced with the server yes, but if we have client A and B and a variable health of type Syncvar client A can have a value of 60 in server and client, but client B has a different value. And this is reasonable.

    Really.. doesn't exist something like what I am searching? just a simple variable where you can consult from a client and share with all clients this variable with the same value... In Java and RMI you can have on the server side variables and methods, and you can use them. But I find Unity so limited than always I am struggling to make progress in my game. Is true that I am new on this, but I had been coding before to know Unity.
     
  4. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    488
  5. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Thank you for your reply. But this is not work for me because I don't have "players" when someone connects to server, my player's character is empty. I just have a host (first client that connects to server) who can move a 3D model (loaded dynamically after first client connection in a new prefab with authority for this client) and other clients that connect later can only see how this first "player" move the model. But the rest of client don't have a character just like that.

    The post you sent me change the variable myColor from server to the script saved on client (which is attached to player) and is a random value, but I need check what is the real value (a client could have changed this variable) and copy the value to local variable on client.

    I don't know if you understand, if so I'll explain better
     
  6. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    161
    Your problem is more of c# problem than Unity.
    The reason each client is getting it's own variable is because you're defining it somehow like this:
    Code (CSharp):
    1. private int val;
    that means, this is instance variable, each instance will have it's own variable.
    If you want all instances of this class to share same variable then you use the static modifier like this:
    Code (CSharp):
    1. private static int val;
    All objects of this class type now will have same value, when one of them changes it, then all instances will see the new value.
     
  7. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    But I am using Syncvar and you can not use static type.. And in the case if you could the variable will be static in the context of the instance of unity. If one player is connected in other PC/mobile the scope of static is reduced to the instance of the "game" in this player, not other players connected trough network
     
  8. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    161
    Then just use a simple client RPC to update the variable on all clients. when value changes an RPC call will change it on all connected clients, new clients will send a request (command/netmessage or whatever you want) to get the variable value on loading.
     
  9. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    This is what I said above. You can call a Rpc method and change the value on all clients, but ONLY THE VALUE THAT YOU OWN, only in your instance of your player on the other players.
    For example, if the server have 3 connected clients and one of them change the value of the variable, this value is changed on the instance of THIS client on other clients, but other clients still having the same value on HIS instance of the player object.

    Context of A:
    A.variable = 1;
    B.variable = 2;

    Context of B:
    A.variable = 1;
    B.variable = 2;

    "A" calls Rpc method changing variable from 1 to 10. Then now we have this:

    Context of A:
    A.variable = 10;
    B.variable = 2;

    Context of B:
    A.variable = 10;
    B.variable = 2;

    Do you see that? this is what I said before. And I need all clients have the same value on the same variable
     
  10. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    I think that getting the gameobject of the first player and consulting the variable I want it will be ok... Im on it
     
  11. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    161
    I was talking about using RPC to change the static variable.
     
  12. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Sure, and I just could use in a no multiplayer game. Thank you anyway
     
  13. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    I am done this on the following way:
    - Considering in our instance of unity are the players connected to server, I saved them in a list.
    - Accessed the first player (is the first that connected to server) and get the data I needed.
     
  14. fedd93

    fedd93

    Joined:
    Oct 15, 2018
    Posts:
    1
    Hey,Please share the code :)
     
  15. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,999
    Kigm hasn't logged into the forums in over 2 years, so don't get your hopes up.