- Home /
Help with the inspector: SyncVar and Serializable
Hello! Semi-newbie here, and I seem to be lost. I'm attempting to create a script-package that can be used for both single-player and multiplayer. I have a manager script for the initial setup of game variables before a match actually begins. I created a list of "teams" in that script so it is easy to create/customize teams in the inspector. In order the team list to show up and be customizable in the manger object's inspector, I had to create this class script...
using UnityEngine;
[System.Serializable]
public class koth_Team {
[Header("Team Options")]
public string teamName;
public Color teamColor;
public int teamScore;
}
The "System.Serializable" allows me to see and change the team's variables in the manager object's inspector. But for multiplayer, I would like to be able to sync the "teamScore" variable. Unfortunately, when I add "NetworkBehavior" to the script so I can SyncVar the "teamScore" variable, the inspector no longer displays the variables. Instead, the inspector just shows empty reference boxes, like it's waiting for me to drag objects into them.
How can I sync the "teamScore" variable while also being able to see and customize the variables in the inspector?
Thank you so much! :-D
Answer by ToasterKyle · May 21, 2017 at 03:06 PM
This worked for me, I don't see how or why it would show empty reference boxes for an integer variable, if it doesn't sync it should just always be at zero or whatever the default value is.
using UnityEngine;
using UnityEngine.Networking;
public class test : NetworkBehaviour {
public koth_Team team;
[System.Serializable]
public class koth_Team
{
[Header("Team Options")]
public string teamName;
public Color teamColor;
[SyncVar]
public int teamScore;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612122510im_/https://answers.unity.com/themes/thub/images/avi.jpg)