Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to pass variables between host and client through RPC?

Discussion in 'Netcode for GameObjects' started by frankiwinnie, Sep 27, 2023.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    27
    Hello everyone,

    I have two variables (int x, int y). In different sessions the x variable has different values (lets say in one x = 5, and in other its x = 10). At some point in the game I need to take the x values in both sessions and assign the values into variable y in other session. So the result has to be x= 5 and y = 10 in one and x = 10 and y = 5 in the other. That seems very basic however I could not figure out how to achieve this though an RPC. Could someone help me with this?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,659
    What do you mean by "session"? Doesn't sound like it's related to Session Management.

    If you need to persist or share data beyond the current game session (eg between different servers/hosts) you'd have to save this data to some cloud storage, a database, Firebase perhaps being the simplest option.
     
  3. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    27

    No, by session I meant the sides in the multiplayer game being the host and client. So I need to pass the local variable by RPC to the local variable of the other players "session".
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,659
    Hmmm so just calling an RPC with a parameter? What have you tried so far?

    You know there is also NetworkVariable<T> to synch any value-type among clients?
     
  5. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    27
    Hi,

    Solved my issue with identfying both variables locally by different functions. I think in case of passing a local variable value to a client we need something specific to the client itself (maybe a bool like isPlayerWonGame). Otherwise I could not figure out how to seperate the clients form one another. Since we use clientrpc it run on all clients so if I make y = 5 it effects on all clients. I don't know if I explained myself correctly, don't mind me if this comes nonsense to you :D