• 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 Ted 1 · May 03, 2011 at 06:45 PM · multiplayervariableserverclientsending

Multiplayer - sending a variable from server to client

Hello,i am pretty new in scripting and i got this problem : The problems appear when i am a client.If i am the server everything is ok.So,as you can see i am trying to send a value from a variable that only the server owns to the client.Only 1 way.Let's say that I want to send the client the current server time.Why if i print DispTemp inside SetVariable function i get the right number(25) and if i print it outside it i get something like : 100 and then 25 and then 100 and then 25 and so on...So this way when i use if(networkView.isMine && Network.isClient){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} i'll always get 100 instead of 25 :S. As far as i noticed,DispTemp = 25 only when !networkView.isMine :S How can i make it to be equal with the value boadcasted by the server all of the time? Please help me and thanks in advance.An anwser in JavaScript would be welcome aswell :D

using UnityEngine; using System.Collections;

public class MonoTestVar : MonoBehaviour { int Temp = 25; int DispTemp = 100;

void Update(){ Debug.Log(DispTemp);

if(Network.isServer && networkView.isMine){ networkView.RPC("SetVariable", RPCMode.All, Temp);} }

[RPC] void SetVariable(int newTemp){ //This RPC is in this case always called by the server, // but executed on all the clients DispTemp = newTemp;

} void OnGUI(){ if(networkView.isMine && Network.isClient){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} if(networkView.isMine && Network.isServer){ GUI.Box(new Rect(0,0,Screen.width / 16,Screen.height / 32),DispTemp + "C");} } }

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

Answer by AKAssassin · May 03, 2011 at 07:14 PM

If you want the server to change the value all of the time then take out the check networkView.isMine.

You wouldn't want to check that because even though the code is running on the server he still may not actually own the networkview, so you would just check.

if(Network.isServer){
    networkView.RPC("SetVariable", RPCMode.All, Temp);}
}

That way the server will always update the value even if it doesn't own the networkView on the object.

Comment
Add comment · Show 2 · 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 Ted 1 · May 03, 2011 at 08:54 PM 0
Share

Yeah,i tried that before but if i want Temp to increase like if(Network.isServer){ Temp += 1; networkView.RPC("SetPosition", RPC$$anonymous$$ode.All, Temp);} }

It will give me again 2 values : 1 is the one calculated by the server and the other one is calculeted by the client :S Please help me fix this :(

avatar image Ted 1 · May 04, 2011 at 04:28 PM 0
Share

Baisicaly if i put a debug.log inside the SetVariable function DispTemp = Temp(the one calculated by the server) BuT if i place it in the update function for instance i'll get it's default value...Why is that happening?

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

No one has followed this question yet.

Related Questions

Unity networking tutorial? 6 Answers

attached prefabs before client joins 2 Answers

Stop host without kick clients 1 Answer

Why can't the client move a networked object ? 1 Answer

I need Help with Multiplayer I can server but not use client 3 Answers

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