Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ElChileVengador · Sep 12, 2015 at 09:35 AM · networkingsync

uNet sync issue - Beginner's problem

Hi everbody,

I'm new to uNet and I'm having problems with syncing variables to the host. I have updated Unity to the latest patch, followed the UNET tutorial and read the documentation, but I'm sure something slipped by and I haven't gotten this code to run. I'm trying to get both the host and a client to control a single variable.

The code is very simple, every time I do a right click the value is increased by 1; when I do a left click it should be synced up. Here's the code

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 public class NetworkTestScript : NetworkBehaviour {
     
     [SyncVar(hook = "HookTest1")] 
     private int testValue1;

    // I just use this variable to make the sync variables visible in the inspector
     public int showTest1;
     public int myValue;
 
     // initializing with some number
     void Awake()
     {
         testValue1 = -90000;
     }
 
     // the controls
     void Update()
     {
         if (isLocalPlayer) {
             if (Input.GetMouseButtonUp (0)) {
                 UpdateTheData();
             }
             if(Input.GetMouseButtonUp(1))
             {
                 myValue ++;
             }
         }
 
         showTest1 = testValue1;
     }
 
     void UpdateTheData()
     {
         TransmitValue ();
     }
     
     [Command]
     void CmdGiveStringToServer(int _myVal)
     {
         Debug.Log (" my name is "+name+ " I am the Server: "+ isServer+" I am local: "+ isLocalPlayer +" the value recieved is "+ _myVal);
         testValue1 = _myVal;
 
         Debug.Log ("Values are now "+ testValue1);
     }
     
     [ClientCallback]
     void TransmitValue()
     {
         Debug.Log ("Transmitting from "+ name +" the value of "+ myValue);
         CmdGiveStringToServer( myValue);
     }
 
     //this just lets me know when it was modified.
     void HookTest1(int _var)
     {
         Debug.Log ("the test1 variable has been synced to "+ _var);
     }
 }


However, even after the hook is activated, the value in the server does not change alt text while this appeared in the command line, the inspector still showed a value of 2.

when I tried updating the value from the client this happened: alt text

Notice how both objects say they are the server. The value updated but only in that object, not syncing with the host. Likewise, when I change the value of the host, it does not change in the client.

I know I must be forgetting something really simple, but I can't place a finger on it. Do you guys know what I might be doing wrong?

screen-shot-2015-09-11-at-33942-pm.png (37.8 kB)
screen-shot-2015-09-11-at-34010-pm.png (21.2 kB)
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 Erdroy · Sep 12, 2015 at 10:37 AM 0
Share

Did you try to assign manually testValue1 in HookTest1? Sth. like: testValue1 = _var;

avatar image ElChileVengador Erdroy · Sep 12, 2015 at 11:24 PM 0
Share

Hi Erddoy! Thanks for your response. Unfortunately the result is the same. The value only changes on its own script, not on the others. It's almost as if it were treating both as servers.

Just as additional information, the script is attached to the player character and spawned using the Network manager player prefab.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ElChileVengador · Sep 15, 2015 at 02:53 AM

Found the answer; so the code is actually working fine; it is the way that I was reading it was wrong. I was expecting the [syncVar] to change across all instances of the script, not just the one in that particular gameobject. Thanks to everybody who read the question.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

UNET Server not setting [SyncVar] 0 Answers

Networking Sync SetActive Not Working 0 Answers

How to sync network 0 Answers

How fast do Syncvars synchronize? 1 Answer

How to Sync List 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