• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alemimi · Dec 26, 2013 at 05:54 PM · networkvariablehowtosend

How to pass variables in a Network using RPC function(s)

This should be an easy question to anyone who tried to make a Multiplayer game;

Please, can anyone tell me in a clear way how to use RPC functions?

From what I understood about it, the RPC function works like this;

 using UnityEngine;
 using System.Collections;
 
 public class ExampleRPC : MonoBehaviour {
 
     int number = 120; //the number I want to send to another machine using RPC.
     public static bool somethingHappens = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(somethingHappens){ //If anything happens that makes this variable true:
             networkView.RPC("CallThisFunctionWithRPC", RPCMode.All, number); //Send the variable to anything that has a "CallThisFunctionWithRPC" function(?)
         }
     }
 
     [RPC] void CallThisFunctionWithRPC(int newNumber){ //The function
           //And here? How do I tell in this function to make the "number" of the old machine become the "number" of the new machine?
           number = newNumber; //(?)
     }
 }

I already tried doing this and it didn't work, the variable was 120 all the time even if I changed it ingame, the 2 variables on the 2 different builds were not synchronyzed.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ziv03 · Dec 26, 2013 at 06:10 PM 0
Share

That's because the int "number" is a private int only for ExampleRPC. I'm guessing that when you change it inGame you change a differnt private int in a different class.

The RPC setup is fine.

avatar image alemimi · Dec 26, 2013 at 06:43 PM 0
Share

Yes, actually I succesfully synchronized the 2 variables by adding a few lines of code:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleRPC : $$anonymous$$onoBehaviour {
 
     int number = 120;
     public static bool somethingHappens = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(somethingHappens){
             number = 300;
         }
 
         if(Network.isServer){
             networkView.RPC("CallThisFunctionWithRPC", RPC$$anonymous$$ode.All, number);
         }
 
         if(Network.isClient){
             CallThisFunctionWithRPC(number);
         }
     }
 
     [RPC] void CallThisFunctionWithRPC(int newNumber){
         number = newNumber;
     }
 }

$$anonymous$$aybe calling the RPC function every frame is not the best solution, but it works for me, thank you for answering my question even if you didn't really help me.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Textfield in javascript, make unity read it on pressing enter 1 Answer

How to correctly send a variable to another function? 1 Answer

Assigning varaibles to prefabs 1 Answer

[c#] Basic Network data sending / receiving 1 Answer

How to update unity web player 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges