Okay so the problem i have is that if i add zombies to the scene and then start the game I can kill them fine without a problem. but when i start the game if i spawn them with this code my shoot script no longer applies damage? any ideas?
[ClientRpc]
void RpcSpawnZam()
{
Instantiate(zamby, new Vector3(80, 0, 90), Quaternion.identity);
}
PlayerShoot Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour
{
public PlayerWeapon weapon;
public ZombieHealth zamhealth;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start()
{
if (cam == null)
{
Debug.Log("No Cam");
this.enabled = false;
}
if (zamhealth == null)
{
Debug.Log("No Zombie");
}
// heart = GameObject.Find("Heart").GetComponent <HeartTest > ();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
[Client]
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, weapon.range, mask))
{
if (hit.transform.tag == "Zombie")
{
CmdShot(hit.collider.name, weapon.damage);
}
else
{
Debug.Log("Shot but Zombie not found");
}
}
}
[Command]
void CmdShot(string _id, int damn)
{
if (zamhealth == null)
{
zamhealth = GameObject.Find(_id).GetComponent<ZombieHealth>();
Debug.Log("shoot set to "+_id);
}
//Debug.Log(_id + " was hit for "+damn);
//HeartTest.HeartDamage(damn);
//dammage
// heart.HeartDamage(4);
zamhealth.RpcZombieDamage(100);
Debug.Log(_id+ " should be hit for 100");
}
}
Zombie Health Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ZombieHealth : NetworkBehaviour
{
[SerializeField]
private int ZombieMaxHealth = 100;
[SyncVar]
private int ZombieCurrentHealth;
// Use this for initialization
void Start () {
ZombieCurrentHealth = ZombieMaxHealth;
}
// Update is called once per frame
void Update () {
CmdDestory();
Debug.Log(gameObject.name + " health = " +ZombieCurrentHealth);
}
[Command]
void CmdDestory()
{
if (ZombieCurrentHealth < 1)
{
Debug.Log("should be fucken dead");
Destroy(gameObject);
}
}
[ClientRpc]
public void RpcZombieDamage(int dam)
{
ZombieCurrentHealth -= dam;
}
}