- Home /
Question by
Rtsgamerx · Mar 10, 2019 at 12:34 PM ·
networkingbugupdate
Script acting weirdly after updating from 2018.2.2 to 2018.3.1
Everything was working fine util I updated to Unity 2018.3.1. Here you can see my player script. The problem is when a player die, he dies on the instance of the player who kills him, but is still alive in his instance. And when he respawns he sometimes respawn with over 100 hp or less.This scripts is attached to the player with a networkIdentity:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Player : NetworkBehaviour {
public int MaxHealth = 100;
[SyncVar]
private bool _isDead = false;
public bool isDead
{
get { return _isDead; }
protected set { _isDead = value; }
}
[SerializeField]
private Behaviour[] DisableOnDeath;
[SerializeField]
private GameObject[] DisableGameObjectsOnDeath;
private bool[] wasenabled;
public void PlayerSetup() {
wasenabled = new bool[DisableOnDeath.Length];
for (int i = 0; i < wasenabled.Length; i++)
{
wasenabled[i] = DisableOnDeath[i].enabled;
}
SetDefaults();
}
public void SetDefaults()
{
isDead = false;
CurrentHealth = MaxHealth;
for (int i = 0; i < DisableOnDeath.Length; i++)
{
DisableOnDeath[i].enabled = wasenabled[i];
}
//Enable GameObjects
for (int i = 0; i < DisableGameObjectsOnDeath.Length; i++)
{
DisableGameObjectsOnDeath[i].SetActive(true);
}
Collider col = GetComponent<Collider>();
if (col != null)
{
col.enabled = true;
}
Playerui.SetActive(true);
//Make SpawnEffect
GameObject gfxIns = Instantiate(SpawnEffect, transform.position, Quaternion.identity);
Destroy(gfxIns, 3f);
}
[ClientRpc]
public void RpcTakeDamage(int amount)
{
if (isDead)
return;
CurrentHealth -= amount;
Debug.Log(transform.name + " has now " + CurrentHealth + " health");
if (CurrentHealth <= 0)
{
Die();
}
}
IEnumerator Respawn()
{
yield return new WaitForSeconds(GameManager.instance.matchSettings.RespawnDelay);
Transform _startpoint = NetworkManager.singleton.GetStartPosition();
transform.position = _startpoint.position;
transform.rotation = _startpoint.rotation;
yield return new WaitForSeconds(0.1f);
SetDefaults();
Debug.Log(transform.name + " Respawned");
}
public void Die()
{
isDead = true;
//DisableComponenents
for (int i = 0; i < DisableOnDeath.Length; i++)
{
DisableOnDeath[i].enabled = false;
}
//DisableGameObjects
for (int i = 0; i < DisableGameObjectsOnDeath.Length; i++)
{
DisableGameObjectsOnDeath[i].SetActive(false);
}
//Disable the collider
Collider col = GetComponent<Collider>();
if (col != null)
{
col.enabled = false;
}
Playerui.SetActive(false);
Debug.Log(transform.name + " is dead");
//SpawnDeathEffect
GameObject gfxIns = Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gfxIns, 3f);
//Call respawn method;
StartCoroutine(Respawn());
}}
Comment
Your answer
Follow this Question
Related Questions
UNET "no free events for message" when client pauses game. 2 Answers
Threading with UDPClient - Editor Freezing 2 Answers
Network.Connect Fails To Connect 0 Answers
[UNET] Raycast Update lag when Client connect 1 Answer
Unity networking tutorial? 6 Answers