I want to load a prefab that I added in a custom local package, and it fails (returns null). I am using
var prefab = Resources.Load<Boat>("Packages/com.company.module/Assets/Resources/Boat.prefab");
I got the prefab path from my project by right clicking on the prefab and selecting “Copy Path”. I also checked that the path is part of UnityEditor.AssetDatabase.GetAllAssetPaths().
I used to load this exact prefab fine when it was in the project Asset/Resources folder, but now I have moved it to a custom package and it doesn’t load anymore.
What could go wrong? Is there a way to get some more detailed error log, or inspect a build structure and see what’s wrong?
Note: if I create a public variable in my scene’s script and drag and drop the prefab from the package to that variable (in the Editor UI), it instantiates just fine. I would prefer to load by code though.
public class SceneScript: MonoBehaviour
{
public Boat boatPrefab2;
void Start()
{
var boatPrefab1 = Resources.Load<Boat>("Packages/com.company.module/Assets/Resources/Boat.prefab");
// boatPrefab1 is set to null
Boat boat2 = Instantiate(boatPrefab2, boatsParent.transform);
// instantiation works fine
}
}