- Home /
Unet Not Syncing Health for Host
I know unet is deprecated, but I still decided to use it as I couldn't find a very good tutorial on the new system and I had already started my project using unet. All I am trying to do is have a bullet do a certain amount of damage to a player when it hits them. The health variable is a syncvar but it still isn't syncing to the network. I don't know if the health should be controlled by a non-player object and if it did, i don't know how I would do that. If the host were to shoot the other client enough times, then that client would die and it shows up on both the host's screen and that client's screen. But, however, if the client were to shoot the host enough times, then the host would die for the client but not for the host. Whenever the client shoots a bullet I get an error that I cannot NetworkServer.Spawn a null object, but it still spawns the bullet. I don't know if that has anything to do with the fact that the client can't kill the host.
Here is my health script (attached to all players):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerHealthM : NetworkBehaviour
{
public GameObject deathEffect;
[SyncVar]
public float health = 100;
float startHealth;
float damageTaken;
private void Start()
{
CmdSetStartHealth();
}
void CmdSetStartHealth()
{
startHealth = health;
}
public void CmdTakeDamage(float damage)
{
Debug.Log("Took Damage");
damageTaken = damage;
health -= damageTaken;
if (health <= 0)
{
CmdDie();
}
}
private void Update()
{
if (transform.position.y <= -4)
{
CmdDie();
}
}
public void CmdAddHealth(float amount)
{
health += amount;
if (health > startHealth)
{
health = startHealth;
}
}
private void CmdDie()
{
GameObject effect = Instantiate(deathEffect, transform.position, deathEffect.transform.rotation);
Destroy(gameObject);
}
[Command]
public void CmdMakeTheTake(float damage)
{
CmdTakeDamage(damage);
}
}
Here is my bullet script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BulletM : NetworkBehaviour
{
public float speed = 20f;
public float Damage;
public Rigidbody2D rb;
public GameObject impactEffect;
bool canTakeDamage = true;
public GameObject creator;
GameObject instC;
void Start()
{
Destroy(gameObject, 5f);
rb.velocity = transform.right * speed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
PlayerHealthM ph = collision.gameObject.GetComponent<PlayerHealthM>();
if (collision.gameObject.tag == "Player")
{
Debug.Log("GotHIt");
ph.CmdMakeTheTake(Damage);
}
if (collision.tag != "Bullet")
{
GameObject effect = Instantiate(impactEffect, transform.position, impactEffect.transform.rotation);
instC = Instantiate(creator);
instC.GetComponent<Shoot>().Cmdshoot(effect);
Destroy(instC);
Destroy(gameObject);
}
}
}
Thanks in advance!
Answer by SirOrange · Mar 11, 2019 at 12:47 AM
Well, not all of your "Cmd" voids have the [Command] above them... try Adding that.