• 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 danlinenberg · Feb 23, 2014 at 08:58 PM · networkvariablesrpc

Regarding RPC variables

Hello, I have a question regarding RPC variables in a network, because I'm abit confused - I'm making an online rock-paper-scissors game-

I have 2 gameobjects in the scene, each one controlled by a different client. on both gameobjects there's the same script with networkview activated.

I have 2 private variables in the script, one for each player decision. I use an RPC function to update these variables, depending on the tag of the gameobject - so for example a player controls the gameobject with the tag "player2", when he makes a decision - the function updates the variables that correspondes to player2.

the problem is - the clients won't recongnize the variables of the other clients - so if i'm player1, I can't access the variable of player2, and vise versa.

Am I missing something? how can I get all clients access to every variable in the script?

Comment
Add comment · Show 3
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 Corentin · Feb 23, 2014 at 09:23 PM 0
Share

What do you mean with RPC variables ? RPC means remote procedure calls, they are just a way to call a function on a remote computer... Are you talking about arguments ? If you call some RPC with an argument, the RPC will be performed with this same argument value on every computer in RPC$$anonymous$$ode

avatar image Kiloblargh · Feb 23, 2014 at 09:46 PM 0
Share

There are a limited number of types of variables that can be sent as arguments to an RPC. Are you trying to send something like a GameObject or $$anonymous$$aterial as an argument? Because that won't work.

But that's just a guess. Post the script you're talking about if you want someone to be able to actually give you an answer.

avatar image danlinenberg · Feb 23, 2014 at 09:54 PM 0
Share

I'm giving strings as arguements... here are the relevant pieces of the code:

 private string dp1;
 private string dp2;
 
 void OnGUI() { 
 if(GUI.Button(new Rect((100,100,,150,75),shoot))
     networkView.RPC("SetDecision", RPC$$anonymous$$ode.AllBuffered, "Shoot"); } 
 
 [RPC] void SetDecision (string decision) {
 if(this.tag=="Player1") 
     dp1=decision; 
 if(this.tag=="Player2")
     dp2=decision; 
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by YoungDeveloper · Feb 23, 2014 at 10:02 PM

RPC sends the message to same gameobject in others scene. The variables are updated, but not for the "actual" player, but clone he is seeing. Simplest solution would be having gameobject which caches the gameobject of the new spawned-joined player which actually owns the scene. When rpc is received you then actually change that data on real player gameobject.

If i understanded your problem correctly.

Comment
Add comment · Share
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
0

Answer by Kiloblargh · Feb 23, 2014 at 10:18 PM

The RPC is just sending the string "Shoot", which contains no information.

Shouldn't it be sending "Rock", "Paper" or "Scissors?"

Also, you should use RPCMode.Others and change

 if (this.tag=="Player1") 
     dp1=decision; 
 if (this.tag=="Player2")
     dp2=decision; 
 }

to

 if(this.tag=="Player1") 
     dp2=decision; 
 if(this.tag=="Player2")
     dp1=decision; 
 }

You can set the players' own decision locally before sending the RPC.

Comment
Add comment · Show 3 · Share
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 danlinenberg · Feb 23, 2014 at 10:35 PM 0
Share

the shoot is just a notation, it could be "rock" for example.

I think I didn't explain myself fully or I didn't understand your answer-

Each player has his own GUI. If player1 for instance clicks on "rock", I would like to change the variable dp1 to "rock", across all clients. So dp1 stands for the decision of player1, and dp2 stands for the decision of player2. I'd like to be able to change and access these variables across the clients.

The problem right now is that If i am player1, and player2 makes a decision, the variable "dp2" remains null for player1 even after the change...

avatar image Kiloblargh · Feb 24, 2014 at 03:08 AM 0
Share

If it remains null, that probably means your real problem is that you don't have a networkView on the object with the script, or they're not set up with the same ID. The problem I would expect from your code is that both change to the same value.

You should have a GameController script on a faceless gameObect for each player that has the one and only networkView in the game. (If there's only one, you don't need Network.Instantiate, and you don't need to worry about the NetworkViewID).

Then each button should send the GameController script a message, for example, gameController.SendThrow ("rock");

And the SendThrow(String decision) function first sets the local player's (dp1 or dp2) variable to "rock", then calls "networkView.RPC("ReceiveThrow", RPC$$anonymous$$ode.Others, "rock"); which will set the remote (dp1 or dp2, depending) variable to "rock" too.

avatar image danlinenberg · Feb 24, 2014 at 05:51 PM 0
Share

ok, i'll give it a try. thanks :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image

Related Questions

How to pass a variable to a late-joining player without syncvars? / What function gets called on a new player connect? 1 Answer

Send RPC in 2 scene 0 Answers

RPC calls client to server Errors 1 Answer

[Solved]Send rate of rpc is slow even if Network.sendRate = 15; 1 Answer

Getting rotation to work on client 1 Answer


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