AssetDatabase.LoadAssetAtPath Help! Can't Get It Working

Hello!

I’m making a game with a combat system similar to final fantasy (old ones) or pokemon. The problem I’m having is I will at some point have to choose from many monsters to choose from for combat and then sort their turn order through scripting.

I’m a beginner at game making/scripting and my thought is that in the future I will have a large database of monsters and information to choose from which I don’t want to do through the inspector as it will be clunky and onerous.

I have tried using AssetDatabase.LoadAssetAtPath to find the prefab I have made for the monster and instantiate it into the scene. However this returns null. Code below.

 Instantiate(AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Monsters/Orc", typeof(GameObject)));

My questions in a condensed form.

  1. What am i doing wrong with the code above? Why is it returning null? Ideally this would create the prefab I have made of an orc.

  2. Is this the best way to manage assets? I only really need the script thats attached to the orc which will contain all his variables, sounds, images etc. Imagine if I have 100, 200, 500… Monsters is making a prefab for each and attaching a copy of the base stats to them be the best way to deal with the monster database.

Thanks for any help in advance and am happy to listen to any recommendations on how to optimize the process going forward.

Cheers,

Question 1: Unlike with Resources.Load(), I’m pretty sure with AssetDatabase.LoadAssetAtPath you need to supply the full path, including the .prefab extension.

Question 2: Look into ScriptableObjects… they are just data bags, perfect for instances of something like “Stats.” Then you can have a single prefab with a single monster controller on it, and that controller takes a reference to a Stats object that is a scriptable object, and you can make as many Stats objects as you like. They are editable in the Unity editor, they may contain snippets of code, you can clone and copy them, etc.

In practice you probably want broad classes of stats, like “MobilityStats” and “CombatStats” and “MagicStats”, etc. But start simple with one bag, see how it goes. When you make a ScriptableObject type there’s a handy decorator you can put above the class that makes it right-click-makeable in the Editor project window, pretty spiffy.

Thank you! I will try this when I’m back at the desk.

Regards

No you don’t want AssetDatabase.LoadAssetAtPath, that’s for editor scripts and won’t work in a build. (Anything that requires using UnityEditor is for adding editor functionality, not game functionality).

Instead you’ll want to check out Resources.Load:Unity - Scripting API: Resources.Load

It only loads assets from the ‘Resources’ folder, so move any prefabs you want to load at runtime into a ‘Resources’ folder. (ie. “Resources/Prefabs/Monsters/Orc”)

Then you load and instantiate like so:

Monster orcPrefab = Resources.Load("Prefabs/Monsters/Orc") as Monster;
Monster orcInstance = Instantiate(orcPrefab);

Note the path used is “Prefabs/Monsters/Orc”, and not “Resources/Prefabs/Monsters/Orc”.

1 Like

Hey OP, I think I misread your question. The post above by @ghostmode is correct: what you posted will only work in-editor, which I assumed was your intent.

If you are trying to get it running at game run time, then as @ghostmode is correct: Resources.Load is your function.

Thanks team! Got it working. Will look into scriptable objects now.

Cheers all

1 Like