Instantiating Prefabs

Discuss modding questions and implementation details.
Post Reply
mwitz
Posts: 9
Joined: Mon May 30, 2022 8:17 am

Instantiating Prefabs

Post by mwitz »

I've set up a mod and I'm trying to instantiate some prefabs; specifically, prefabs of GameObjects with scripts attached. The mod runs fine in the editor, but when I build the mod and try running it in the Daggerfall Unity standalone, I get the error "ArgumentException: The Object you want to instantiate is null." I haven't been able to figure out why the prefab is null. I also double checked that my prefabs are selected in the mod builder. Is there something specific you're supposed to do with prefabs to be able to instantiate them in your mod?

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Instantiating Prefabs

Post by l3lessed »

Yes, if you haven't setup the code to initiate it, it won't work.

Here is an example of how to setup the empty objects and then initiate my prefab into them. I provide comments explaining it.

Code: Select all

public class AmbidexterityManager : MonoBehaviour
{
	//declare the game object to initiate the prefab to. Here I call it SparkPrefab since it contains my spark particle system.   
	public GameObject SparkPrefab;

	//Now, declare the specific object type you're loading from the prefab you just initiated. Here its a ParticleSystem object since I'm grabbing my custom particle system that creates the spark effect.
	public static ParticleSystem sparkParticles;          

	//now use the start routine to initiate the actual prefab into the empty objects just declared in the properties area.
	void Start()
	{            
		//grabs and assigns the spark particle prefab from the mod system and assigns it to SparkPrefab Object.
		//MAKE SURE THE NAME,("Spark_Particles"), MATCHES THE PREFABS NAME OR IT WILL NULL ERROR YOU.
		SparkPrefab = mod.GetAsset<GameObject>("Spark_Particles");

		//now it grabs the particle system from the SparkPrefab object and assigns it to the specific particle system object for later use.
		sparkParticles = SparkPrefab.GetComponent<ParticleSystem>();
	}
	
	//CONTROLS PARRY ANIMATIONS AND WEAPON STATES\\
	//if player is hit and they are parrying do...
	if (isHit && AttackState == 7)
	{	
		//I use a timed destroy combined with an initiate call to create the spark particle from the prefab and then have it destroyed automatically after one second.
        	Destroy(Instantiate(sparkParticles, attackerEntity.EntityBehaviour.transform.position + (attackerEntity.EntityBehaviour.transform.forward * .35f), Quaternion.identity, null), 1.0f);		
	.... 
	}
}
At this point, I just call the sparkParticle particle system object to create and control a weapon spark on the fly.

If you're code is setup right, it is still isn't initiating, it could be the result of the mod system having issues grabbing whatever custom thing is in your prefab. I think I had this issue once myself and had to just build something from straight code and scripts to get around it.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

Post Reply