FPS ERROR WHEN TRYING TO RESPAWN

Updated on May 10, 2017 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on March 6, 2017

i am following Making a Multiplayer FPS in Unity series and i got stuck as i was watching Episode 8, where everything worked so smooth up until when he created the MatchSettings script,modified the PLAYER script including The GameManager ,the player dies when you shoot him but Respawning doesn’t work any more ,as it brings up an error

THE ERROR STATES THAT

NullReferenceException: Object reference not set to an instance of an object
Player+<Respawn>c__Iterator0.MoveNext () (at Assets/Scripts/Player.cs:84)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Player:Die() (at Assets/Scripts/Player.cs:79)
Player:RpcTakeDamage(Int32) (at Assets/Scripts/Player.cs:60)
Player:InvokeRpcRpcTakeDamage(NetworkBehaviour, NetworkReader)
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

THIS IS THE PLAYER.CS


using UnityEngine;

using UnityEngine.Networking;

using System.Collections;
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]

private int currentHealth;
[SerializeField]

private Behaviour[] disableOnDeath;

private bool[] wasEnabled;
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;
if (Input.GetKeyDown(KeyCode.K))

{

RpcTakeDamage(99999);

}

}
[ClientRpc]

public void RpcTakeDamage(int _amount)

{

if (isDead)

return;
currentHealth -= _amount;
Debug.Log(transform.name + " now has " + currentHealth + " health.");
if (currentHealth <= 0)

{

Die();

}

}
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!");
StartCoroutine(Respawn());

}
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;
for (int i = 0; i < disableOnDeath.Length; i++)

{

disableOnDeath[i].enabled = wasEnabled[i];

}
Collider _col = GetComponent<Collider>();

if (_col != null)

_col.enabled = true;

}
}

  • Liked by
Reply
0 on March 6, 2017

Your error says that the for loop in your Die method is returning null.
Make sure that the array has been populated correctly.
Alternately, add a conditional that checks if it’s null first.

Let me know if this helps!

  • Liked by
Reply
Cancel
2 on March 7, 2017

So far i have tried to populate the array correctly ,and i am still getting the same problem
.Do you mean checking if the die method is Null? if so how do i go about it? i used used this stupid way and it didn’t work

 if (Die = null) 
 {
 Debug.Log ("Player can't Repsawn!");
 } else 
 {
 StartCoroutine (Respawn ());
 }

Please help I am stuck!

Guru
on March 7, 2017

Your method can’t work since it has only one = which sets the variable to null you want to use ==

Devoted
on March 7, 2017

I was meaning checking if the component you are trying to enable/disable is null.
E.g
if(disableOnDeath[i] != null)
disableOnDeath[i].enabled = false;

Show more replies
  • Liked by
Reply
Cancel
0 on April 29, 2017

I have the same problem what can I do?

  • Liked by
Reply
Cancel
0 on May 10, 2017

I had the same problem. Just close Visual studio and open it again and it will fix it (at least it worked for me). Sometimes Visual studio and Unity don’t sync well, so that’s why you get this problem.

  • Liked by
Reply
Cancel