Photon Unity Networking: DefaultPool failed to load "playerPrefab"

70 Views Asked by At

I'm trying to instantiate a player using Photon in Unity 2D, but I'm encountering two errors that I can't seem to resolve:

Error 1: DefaultPool failed to load "playerPrefab". Make sure it's in a "Resources" folder. Or use a custom IPunPrefabPool.

I've double-checked that the playerPrefab is in a "Resources" folder, and the folder is named correctly. I'm not sure why this error is occurring.

Error 2: NullReferenceException: Object reference not set to an instance of an object

Here's the relevant portion of my code:

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

public class PlayerSpawner : MonoBehaviourPun
{
    [SerializeField] private Transform[] spawners;
    [SerializeField] private GameObject playerPrefab;

    void Start()
    {
        GameObject player = PhotonNetwork.Instantiate(nameof(playerPrefab), Vector3.zero, Quaternion.identity);
    }
}

I have ensured that all references are set in the Unity Editor, and the playerPrefab is assigned correctly. What could be causing these errors, and how can I resolve them?

Any help or guidance would be greatly appreciated. Thank you!

1

There are 1 best solutions below

1
Philip John On
using UnityEngine;
using System.Collections.Generic;
using Photon.Pun;

public class PreparePool : MonoBehaviour {
    public List<GameObject> Prefabs;

    private void Start() {
        DefaultPool pool = PhotonNetwork.PrefabPool as DefaultPool;
        if(pool != null && this.Prefabs != null)
        {
            foreach(GameObject prefab in this.Prefabs)
            {
                pool.ResourceCache.Add(prefab.name, prefab);
            }
        }
    }
}

Normally, Resource folder already added to PhotonNetwork.PrefabPool.

You can fix this error if you use above code and There is no need to copy it to the resource folder.

Hope help this answer for you