I can’t find a tutorial/example of how to spawn a prefab in game. If anyone can help, that would be great.
Check out the docs. They have some pretty good examples! https://docs.flaxengine.com/manual/get-started/prefabs/spawning-prefabs.html?tabs=code-csharp. Basically you will want to have a member variable in a script of type Prefab
and then use the PrefabManager.Spawn()
function to spawn the prefab.
To spawn prefabs, you can’t use
Actor objToSpawn;
GameObject objToSpawn;
But you’ll need to use:
Prefab objToSpawn;
Then call the spawn method from PrefabManager:
override void Start()
{
PrefabManager.Spawn(objToSpawn, new Vector3(0, 0, 0), Quaternion.Indetity);
}
Does the type have to be Prefab, or can it be the name of the Prefab?
The type will have to be Prefab and then you can select your prefab in the editor.
Type matters, actually not the name.
If your Prefabs name is, “AI Base”
You can’t do
AIBase objToSpawn
You still use name
Prefab objToSpawn
Don’t forget to use
[Serialize, ShowInEditor]
Otherwise, you can’t assign the Prefab to objToSpawn in the Flax Editor.
Do something like this:
[Serialize, ShowInEditor]
Prefab objToSpawn
override void Start()
{
PrefabManager.Spawn(objToSpawn, new Vector3(0, 0, 0), Quaternion.Indetity);
}
I hope it makes you clear
Thanks for the excellent summary. Yes, it is very clear now.