0

So I have a NullReferenceException I can't see the problem with. I'n the Debug Log I get:

/Game Projects/LaZeR/Assets/Scripts/PlayerSpawner.cs Line: 37)

NullReferenceException: Object reference not set to an instance of an object
  at PlayerSpawner.Die () [0x00001] in C:\--\--\Game Projects\LaZeR\Assets\Scripts\PlayerSpawner.cs:37 

but the code on line 37 looks fine to me:

PhotonNetwork.Instantiate(deathEffect.name, player.transform.position, Quaternion.identity);

Can anyone steer me in the right direction? I'm still quite green so I'm just missing little things, I appreciate everyone's patience with me :)

Here is the full code, 42 lines in total:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class PlayerSpawner : MonoBehaviour
{
    public static PlayerSpawner instance;

    private void Awake()
    {
        instance = this;
    }

    public GameObject playerPrefab;
    private GameObject player;
    public GameObject deathEffect;
    // Start is called before the first frame update
    void Start()
    {
        if(PhotonNetwork.IsConnected)
        {
            SpawnPlayer();
        }

    }

    public void SpawnPlayer()
    {
        Transform spawnPoint = SpawnManager.instance.GetSpawnPoint();

        player = PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, spawnPoint.rotation);
    }

    public void Die()
    {
        PhotonNetwork.Instantiate(deathEffect.name, player.transform.position, Quaternion.identity);
        PhotonNetwork.Destroy(player);
        SpawnPlayer();
    }

}
  • 2
    One of them is `null`. – mjwills Aug 16 '21 at 02:55
  • 1
    It seems that `deathEffect` is null, `player` is null, or `player.transform` is `null`. – ProgrammingLlama Aug 16 '21 at 03:01
  • Either the `player` or `deathEffect` is `null` at the time of execution. Ensure that `SpawnPlayer` is invoked before `Die` and `deathEffect` is assigned in the inspector. – DekuDesu Aug 16 '21 at 03:02
  • 1
    SOLVED: `deathEffect` just needed some work, and the animation needed to be added in Unity. It is now working, thank you all <3 – Mikey Kirkpatrick Aug 16 '21 at 03:05
  • I hope you learned how to use the debugger during this exercise. Some of us are old enough to remember when symbolic debuggers were first created. They are a gift from the IDE gods. Use them and smile – Flydog57 Aug 16 '21 at 04:27

0 Answers0