Title: NetworkVariable.OnValueChanged is not called on the client unless the change took place while the object existed on the client · Issue #2318 · Unity-Technologies/com.unity.netcode.gameobjects · GitHub
Open Graph Title: NetworkVariable.OnValueChanged is not called on the client unless the change took place while the object existed on the client · Issue #2318 · Unity-Technologies/com.unity.netcode.gameobjects
X Title: NetworkVariable.OnValueChanged is not called on the client unless the change took place while the object existed on the client · Issue #2318 · Unity-Technologies/com.unity.netcode.gameobjects
Description: Description leaving original title and description for context. NetworkShow does not set NetworkVariables When we call NetworkHide on a dynamically created object, and later call NetworkShow, the object's network variables are not set. N...
Open Graph Description: Description leaving original title and description for context. NetworkShow does not set NetworkVariables When we call NetworkHide on a dynamically created object, and later call NetworkShow, the o...
X Description: Description leaving original title and description for context. NetworkShow does not set NetworkVariables When we call NetworkHide on a dynamically created object, and later call NetworkShow, the o...
Opengraph URL: https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2318
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"NetworkVariable.OnValueChanged is not called on the client unless the change took place while the object existed on the client","articleBody":"### Description\r\nleaving original title and description for context.\r\n\r\n> NetworkShow does not set NetworkVariables\r\n\r\n> When we call NetworkHide on a dynamically created object, and later call NetworkShow, the object's network variables are not set.\r\n> NetworkHide will despawn and destroy the object on the client, so when the server calls NetworkShow, we are now creating a new object on the client, except this time the network variables don't get set.\r\n\r\nUpon further testing, the issue turned out to be that NetworkVariable.OnValueChanged is not being called on the client, UNLESS the value changed while the network object existed on the client.\r\n\r\nSo if the client connects, and the network variable already had its value set on the server, OnValueChanged will not get called on the client.\r\nSimilarly, if the object is network hidden on the client side, when it becomes shown again, OnValueChanged is not called for the network variable; for a dynamically created network object it also gets destroyed on the client, not just network hidden.\r\n\r\nNot sure if the parent hierarchy is playing a role here, but my network object is a child of another network object.\r\n\r\nFor the test, I have a Canvas that's an in-scene network object, then I dynamically (at run time) add a network object child to it (on the server), this network object has the behavior below, then I spawn this network object.\r\n\r\n\r\n```\r\npublic class TestBehaviour : NetworkBehaviour\r\n {\r\n NetworkVariable\u003cfloat> _networkVariable;\r\n\r\n [SerializeField]\r\n private TMP_Text _text;\r\n\r\n private void Awake()\r\n {\r\n _networkVariable = new NetworkVariable\u003cfloat>();\r\n _networkVariable.OnValueChanged += OnNetworkVariableChanged;\r\n }\r\n\r\n private void OnNetworkVariableChanged(float previousValue, float newValue)\r\n {\r\n text.text = _networkVariable.Value.ToString();\r\n }\r\n\r\n private void Update()\r\n {\r\n if (_networkVariable != null)\r\n {\r\n _text.text = _networkVariable.Value.ToString();\r\n }\r\n }\r\n\r\n public override async void OnNetworkSpawn()\r\n {\r\n if (!NetworkManager.Singleton.IsServer)\r\n {\r\n return;\r\n }\r\n\r\n await new WaitForSeconds(10); // give time for client to connect.\r\n _networkVariable.Value = 69; // by now client connected, the client will have OnNetworkVariableChanged raised here.\r\n await new WaitForSeconds(10);\r\n NetworkObject.NetworkHide(1);\r\n _networkVariable.Value = 12; // here we change the value while the object is hidden for the client\r\n await new WaitForSeconds(10);\r\n NetworkObject.NetworkShow(1); // here we show the object to the client, since this object is dynamically created, a new instance will be created, BUT OnNetworkVariableChanged is never raised.\r\n }\r\n }\r\n```\r\n\r\nWhile inspecting this scene on the client side in the editor, I see everything behaving properly except for that OnNetworkVariableChanged method, it does not get raised unless a change took place while the network object existed.\r\n\r\n\r\nThis issue was not present in Netcode v1.0.0, I started experiencing it in v1.1.0\r\n\r\n### Actual Outcome\r\n\r\nNetworkVariable.OnValueChanged is not called on the client unless the change took place while the object existed on the client.\r\n\r\n### Expected Outcome\r\n\r\nNetworkVariable.OnValueChanged should be called when the variable's value changes, regardless if the change took place while the object existed on the client or not.\r\n\r\n\r\n### Environment\r\n\r\n- OS: Win10\r\n- Unity Version: 2022.1.23f1\r\n- Netcode Version: 1.1.0","author":{"url":"https://github.com/TheCaveOfWonders","@type":"Person","name":"TheCaveOfWonders"},"datePublished":"2022-11-19T18:28:27Z","url":"https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/2318"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a730256e-4182-5da7-9312-3f2834f5a5ca |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9228:2ED6B1:396A7C:5080AF:6B005E3C |
| html-safe-nonce | 09b942da9552f3cc472973831a9470762059d1727e9eb12b8feff8170d033fb5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjI4OjJFRDZCMTozOTZBN0M6NTA4MEFGOjZCMDA1RTNDIiwidmlzaXRvcl9pZCI6IjEzNTc5Nzg4NTczMTkxOTQxNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9f2f85376190f369455149317dbd104222517bd56c2a790ceea235b6b36e788e |
| hovercard-subject-tag | issue:1456612073 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Unity-Technologies/com.unity.netcode.gameobjects/2318/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ca2b8f46cd3e0ac5c31eaa2eaa1d8c5bb53b907cb56ddc8e0f58d019aa9b226/Unity-Technologies/com.unity.netcode.gameobjects/issues/2318 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ca2b8f46cd3e0ac5c31eaa2eaa1d8c5bb53b907cb56ddc8e0f58d019aa9b226/Unity-Technologies/com.unity.netcode.gameobjects/issues/2318 |
| og:image:alt | Description leaving original title and description for context. NetworkShow does not set NetworkVariables When we call NetworkHide on a dynamically created object, and later call NetworkShow, the o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | TheCaveOfWonders |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/Unity-Technologies/com.unity.netcode.gameobjects git https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git |
| octolytics-dimension-user_id | 426196 |
| octolytics-dimension-user_login | Unity-Technologies |
| octolytics-dimension-repository_id | 116385203 |
| octolytics-dimension-repository_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 116385203 |
| octolytics-dimension-repository_network_root_nwo | Unity-Technologies/com.unity.netcode.gameobjects |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width