Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

How to update unity web player 0 Answers

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

How to check my Game System Requirements? 2 Answers

What are the prerequisites of using Unity? 1 Answer

How to make player lose lives? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges