• 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 oliver-jones · Nov 13, 2013 at 10:27 AM · networkservershare

Network - Change Varible Across Clients

Hello,

I have a script in my scene that controls the scoring of my game. There is only one instance of t$$anonymous$$s script, and new one IS NOT instantiated when a new player joins.

So there is always one. My question is, if the server makes a change to a variable on t$$anonymous$$s script, will all the clients receive these changes too? Automatically?

So for example, there is a variable called overallScore, in w$$anonymous$$ch the server controls. If the server changes t$$anonymous$$s value to 10, will t$$anonymous$$s value change to 10 on the client scripts too?

My understanding is that, if you have one of somet$$anonymous$$ng in the scene (t$$anonymous$$s script in t$$anonymous$$s case), and a client joins, you technically now have 2 instance of that script and so you need to sync the two together. Correct?

Example:

 if(Network.isServer){
    overallScore = 10;
 }

T$$anonymous$$s will only change the overallScore on the server side to 10, right? Whereas:

 if(Network.isServer){
    networkView.RPC("ChangeScore", RPCMode.All, 10);
 }
 
 @RPC
 function ChangeScore(new : int){
    overallScore = new;
 }

T$$anonymous$$s will be called only by the server, but apply the changes to the clients too. Right?

Do I have to do t$$anonymous$$s all the time? What if I just want to share all the variables on t$$anonymous$$s script, so its the 'centre' of the game?

Comment
Add comment · Show 7
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 nastasache · Nov 13, 2013 at 12:15 PM 0
Share

I think your main RPC schema is correct. As above, ChangeScore() will change the overallScore value automatically on all clients receiving RPC. But this is about already connected clients. I think you have just to use RPCMode.AllBuffered instead of RPCMode.All, as overallScore to be ready also for not yet instantiated scripts.

avatar image KiraSensei · Nov 13, 2013 at 12:17 PM 0
Share

You need to send the information threw the network, so yes.

Just for information, in the method ChangeScore you call the integer "new", you should take another name for the variable, since "new" means something else (colored in blue).

avatar image oliver-jones · Nov 13, 2013 at 02:51 PM 0
Share

Okay, does making a variable private, or public make a difference?

avatar image KiraSensei · Nov 13, 2013 at 03:21 PM 0
Share

Not for RPC calls because RPC calls are made "locally".

avatar image oliver-jones · Nov 13, 2013 at 03:27 PM 0
Share

Do I need a NetworkView on the same gameObject the script is on, and put the Script in the Observer? Thanks

Show more comments

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

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

18 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

Related Questions

How can I check if a particular IP is a server? 1 Answer

Proxy Server 0 Answers

Port not opening? 3 Answers

Server and Network no communication 1 Answer

Syncronization issues between server and client, using the OnStartServer() function, 0 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