我想为服务器启动时生成的对象分配随机数,但是游戏的每个实例都有自己的随机数并且它们不同步,我该如何解决?
public class Health : NetworkBehaviour
{
public Text PlanetHealth;
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
void Start()
{
if(this.gameObject.name == "GrayPlanet(Clone)")
{
currentHealth = Random.Range(0, 50);
PlanetHealth.text = currentHealth.ToString();
}
else
PlanetHealth.text = maxHealth.ToString();
}
public void TakeDamage(int amount)
{
if (!isServer)
{
return;
}
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 55;
}
}
void OnChangeHealth(int currentHealth)
{
PlanetHealth.text = currentHealth.ToString();
}
}