- Home /
Why my networked client Player is not dying ??
following is my code:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Health : NetworkBehaviour
{
public const int maxhealth = 100;
public int Value = 10;
[SyncVar(hook = "OnChangeHealth")] public int currhealth = maxhealth;
public bool DestroyOnDeath;
public RectTransform healthBar;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void takeDamage(int amount)
{
if (DestroyOnDeath)
{
//Debug.Log ("Hii");
//Debug.Log ("Hello");
Destroy(gameObject);
int respawn = GetComponent<EnemySpawner> ().noOfEnemy;
if (respawn == 0) {
GetComponent<EnemySpawner> ().OnStartServer ();
}
}
else
{
currhealth -= amount;
if (currhealth <= 0)
{
AudioSource dead = GetComponent<AudioSource> ();
dead.Play ();
currhealth = maxhealth;
RpcRespawn();
}
}
}
void OnChangeHealth(int health)
{
healthBar.sizeDelta = new Vector2(health * 2, healthBar.sizeDelta.y);
}
[ClientRpc]
void RpcRespawn()
{
if(!isLocalPlayer)
{
transform.position = Vector3.zero;
}
}
}
Using this code My zombie kill player in scene and move to next player but after kill first player sometimes he kill other player sometime gives exception now i dont understand why it is happing please help me
the error is as follow :-
NullReferenceException: Object reference not set to an instance of an object
Health.takeDamage (Int32 amount) (at Assets/Standard Assets/Script/Health.cs:41)
ZombieAttack.CheckIfTargetInRange () (at Assets/Standard Assets/Script/ZombieAttack.cs:43)
ZombieAttack+c__Iterator0.MoveNext () (at Assets/Standard Assets/Script/ZombieAttack.cs:52)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
it says why it is happening. Object reference not set to an instance of an object. the object reference is not set.
But i set the object reference i dot understand why it is not taking the reference actually sometimes it take Reference sometime it won't
hello friend, Actually i started making ga$$anonymous$$g watching unitys multiplayer game tutorial which contain 18 tutorials. now that was finished and i wanted to make enemy follow player and kill him. and i found another tutorial series which contain Enemy following Player and $$anonymous$$illing player.but the thing is both tutorials are different and both have different techniques used now i m stuck because i dont understand where and how to apply changes. i understood what is the problem but dont know how to solve it. Its actually health Script problem.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[UNET] Only spawn certain server objects on local client? 0 Answers
C# How to have a toggle 1 Answer