• 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
Question by NoName115 · May 08, 2014 at 10:20 AM · multiplayernetworkserverclientcommunication

Server and Network no communication

I have a problem with the Network, i create a server and client can connect to it. But i want get some parameters from server to client i tryed t$$anonymous$$s:

public GameObject server; //have tag "Test" and starting value in number is 0

private GameObject finded;

if (GUILayout.Button("StartServer")) {

 server.GetComponent<Test_Script3> ().number = 250;
 Network.Instantiate(server, transform.position, transform.rotation, 1);

}

if (Network.peerType == NetworkPeerType.Client) {

     finded = GameObject.FindGameObjectWithTag("Test");
 if (finded != null) GUILayout.Label("O: " + finded.GetComponent<Test_Script3>().number);

} else if (Network.peerType == NetworkPeerType.Server) {

             finded = GameObject.FindGameObjectWithTag("Test");
             if (finded != null) GUILayout.Label("O: " + finded.GetComponent<Test_Script3>().number);
         }

The main problem is that value of parameter number on the server is 250 but on the client is 0;

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Bunny83 · May 08, 2014 at 11:44 AM

It doesn't work like that. Each player / peer has it's own game world and it's own prefabs. If you change a number on the prefab in the server it won't change the prefab on any other client.

Some basic points about networking in Unity:

  • Only NetworkViews will be able to transfer data from one PC to another.

  • NetworkViews on different PCs / peers are liked by their NetworkViewID. Those with the same ID can communicate with each other

  • A NetworkView can "observe" a built-in component and have it's properties synchronised.

  • Only the NetworkView owner can transmit state-updates, everybody else just listens / receives.

  • Apart from state-synchronisation every NetworkView can be used to transmit RPC calls bi-directional. So a client can send an RPC to the server and the server to the client(s)

  • You can't send RPCs without having a linked NetworkView pair / group. The only indirect exception is Network.Instantiate w$$anonymous$$ch uses an RPC be$$anonymous$$nd the scenes but doesn't require a NetworkView.

  • NetworkViews can also observe a custome script component (aka one of your MonoBehaviour-scripts), but you have to implement the OnSerializeNetworkView callback in that script and specify w$$anonymous$$ch data should be transmitted / received.

  • NetworkViews on objects placed in a scene are linked automatically when the client loads the scene.

I suggest to read through the documentation of NetworkView, Network and the Networking basics. Networking isn't that complicated, but it takes some time until you understand it throughout, so don't rush, read and learn.

edit
I just added a little code snippet that would do what you want in your question: Transferring a number from the Peer that Instantiated the object to all other clients:

When instantiating:

     int num = 250;
     server.GetComponent<Test_Script3>().number = num;
     GameObject instance = (GameObject)Network.Instantiate(server, transform.position, transform.rotation, 1);
     instance.networkView.RPC("SetNumber", RPCMode.AllBuffered, num);

Inside a script on the "server" object you have to declare t$$anonymous$$s method:

     [RPC]
     void SetNumber(int aNum)
     {
         GetComponent<Test_Script3>().number = aNum;
     }

Note: I used a BufferedRPC instead of a "normal" RPC since Network.Instantiate also uses a buffered one. So it's ensured that when another client joins later the value is set as well.

Personally i don't like buffered RPCs all together because they almost force you to use them everywhere and Unity doesn't provide a good way to keep track of / deleting old buffered RPCs.

"Test_Script3" and "number" aren't really meaningful names so we have no idea what kind of information you try to pass to the client. You have to decide from case to case w$$anonymous$$ch kind of RPC is the best one.

Comment
NoName115

People who like this

1 Show 1 · 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 NoName115 · May 09, 2014 at 05:35 PM 0
Share

I work with the Network only few weeks, so i dont really get how it work, but your code really helped me and it is working now. I am so happy and glad that you helped me, thank you so much. Thank you!

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

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

Related Questions

Unity networking tutorial? 6 Answers

Can you tell OnSerializeNetworkView who the owner is? 3 Answers

Smooth network communication like Unity remote 1 Answer

Seperate server - client 2 Answers

Networking RPC calls never sent to other clients 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