Im trying to make a team multiplayer death match. but i’m having a problem with the score part
in my player() class i have die() method and i want to call scoremanager method which contain add teamscore.
the score never updated.
private void Die()
{
isDead = true;
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath*.enabled = false;*
}
Collider _col = GetComponent();
if (_col != null)
_col.enabled = false;
Debug.Log(transform.name + " is DEAD!");
GameObject _GraphicInstance = (GameObject)Instantiate(DeathEffect, transform.position, Quaternion.identity);
Destroy(_GraphicInstance, 3f);
StartCoroutine(Respawn());
//score.AddTeamScore(1);
}
the score manager i make is like this
public class ScoreManager : NetworkBehaviour {
[SyncVar]
public int TeamRedScore=0;
public int TeamBlueScore=0;
public void AddTeamScore(int x)
{
TeamBlueScore += x;
Debug.Log("Blue + 1 ");
}
}
Make sure it’s called on server.
Make sure your variables are marked as SyncVars.
Don’t touch SyncVars on client.
If it still doesn’t work - post your whole code using code tag;
public class Player : NetworkBehaviour
{
[SyncVar]
private bool _isDead = false;
public bool isDead
{
get { return _isDead; }
protected set { _isDead = value; }
}
[SerializeField]
private int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
private int currentHealth;
[SyncVar]
private int currentHealth2;
public RectTransform healthbar;
[SerializeField]
private Behaviour[] disableOnDeath;
private bool[] wasEnabled;
[SerializeField]
private GameObject DeathEffect;
private ScoreManager score;
public void Setup()
{
wasEnabled = new bool[disableOnDeath.Length];
for (int i = 0; i < wasEnabled.Length; i++)
{
wasEnabled[i] = disableOnDeath[i].enabled;
}
SetDefaults();
}
void Update()
{
if (!isLocalPlayer)
return;
else
{
//foreach (GameObject obj in myNetwork.team1)
//{
// if (myplayer == obj)
// {
// Debug.Log("TEAM 1 BABY");
// team = "1";
// }
//}
}
if (Input.GetKeyDown(KeyCode.K))
{
RpcTakeDamage(99999);
}
}
public void RpcTakeDamage(int _amount)
{
if (!isServer)
{
return;
}
if (isDead)
return;
currentHealth -= _amount;
Debug.Log(transform.name + " now has " + currentHealth + " health.");
currentHealth2 = currentHealth;
if (currentHealth <= 0)
{
Die();
}
}
void OnChangeHealth(int health)
{
healthbar.sizeDelta = new Vector2(health, healthbar.sizeDelta.y);
}
[ClientRpc]
public void RpcHealDamage(int _amount)
{
if (currentHealth < maxHealth)
{
if (currentHealth + _amount > 100)
{
currentHealth = 100;
}
currentHealth += _amount;
currentHealth2 = currentHealth;
Debug.Log(transform.name + " now has " + currentHealth + " health." + GetHealthPct());
}
return;
}
private void Die()
{
isDead = true;
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = false;
}
Collider _col = GetComponent<Collider>();
if (_col != null)
_col.enabled = false;
Debug.Log(transform.name + " is DEAD!");
GameObject _GraphicInstance = (GameObject)Instantiate(DeathEffect, transform.position, Quaternion.identity);
Destroy(_GraphicInstance, 3f);
StartCoroutine(Respawn());
score.AddTeamScore(1);
}
private IEnumerator Respawn()
{
yield return new WaitForSeconds(GameManager.instance.matchSettings.respawnTime);
SetDefaults();
Transform _spawnPoint = NetworkManager.singleton.GetStartPosition();
transform.position = _spawnPoint.position;
transform.rotation = _spawnPoint.rotation;
Debug.Log(transform.name + " respawned.");
}
public void SetDefaults()
{
isDead = false;
currentHealth = maxHealth;
currentHealth2 = currentHealth;
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = wasEnabled[i];
}
Collider _col = GetComponent<Collider>();
if (_col != null)
_col.enabled = true;
}
public float GetHealthPct()
{
return (float)currentHealth2 / maxHealth ;
}
}
That is my player code
and i want to call the scoremanager addteamscore when Die()method is called
public class ScoreManager : NetworkBehaviour {
[SyncVar]
public int TeamRedScore=0;
public int TeamBlueScore=0;
[Server]
public void AddTeamScore(int x)
{
TeamBlueScore += x;
Debug.Log("Blue + 1 ");
}
}
but everytime the player die
this error show up
NullReferenceException: Object reference not set to an instance of an object
Player.Die () (at Assets/Scripts/Player.cs:140)
should i put the score manager outside player prefab or not?
I think it should be a rather single object in the scene. Not on prefab.