- Home /
ScriptableObjects and SyncVars, is it posible?
In a RTS project I am working on I am using a ScriptableObject to store faction information. When a game starts and a player is spawned, a faction is assigned to a player object. These factions are predifined and are created inside the editor.
public class Player : NetworkBehaviour
{
[SyncVar]
public Faction faction;
}
public class Faction : ScriptableObject
{
public List<Unit>units;
}
The problem is that the game crashes after starting a multiplayer game. If I remove the SyncVar the game run fine, but the faction won't be synchronized. Is there any way I can synchronize a reference to a Faction or any ScriptableObject? Or do I need to do this my hand with the use of commands and rpcs?
Answer by Bunny83 · Sep 18, 2015 at 11:16 AM
No, you can't use SyncVars in ScriptableObjects. How do you think that would work out? Only objects with a NetworkIdentity component are able to be synced. The SyncVar attribute just acts as a "post-processor directive". So Unity will, after your scripts are compiled, replace those variables with properties and add additional code which uses RPCs / Commands to sync the variable content whenever it's changed.
ScriptableObjects make only sense if you have data which you want to serialize as seperate assets in the editor.
For a network game you should probably have some kind of persistent manager object in your scene. Just make Faction a MonoBehaviour and attach it to an empty child of the manager.
This is unfortunate, but to be expected. SyncVar seemed to do a fair amount under the hood, so it makes sense that it only works with a few types.
.
If ScriptableObject syncing is a must, I'm guessing that this requires some specialized code within the ScriptableObject itself. But I'm not exactly sure what that'd look like quite yet...
Your answer
![](https://koobas.hobune.stream/wayback/20220612033849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Networking synchronize 3DText's 1 Answer
How to get transform syncing over network to work? 1 Answer
SyncVar issue 0 Answers
Variables not sync'd to clients 0 Answers
UNET: Client Host syncing problems 0 Answers