In my current project I have a track prefab that contains a trigger area. When the player reaches the trigger area, it instantiates a prefab of the same track piece, trigger included, ahead, so it continues forever. The problem I’m having is that I need to drag the prefab onto the script component, which is attached to the prefab’s parent object, so it knows to spawn this prefab. This only works once, however, because then of course the prefab itself doesn’t have the prefab with the script attached attached. Can anyone help me? Sorry if this doesn’t make sense, I’d be glad to explain more if needed.
Good day.
You can load prefabs just by code, not using the inspector, as a private variable. For do this, you need to create a folder in your assests folder called “Resources”
Like this
–
–
Lets imagine you have a GameObject type prefab called “Box” in your Resources folder in Assets. So you can load the prefab like this:
GameObject BoxPrefab;
BoxPrefab = Resources.Load("Box") as GameObject;
Instantiate (BoxPrefab, position, rotation);
So you can make the instantiated prefab, to load all hte prefabs it needs just by adding this to any of its scripts. You dont need the inspector to assing the prefab!
Bye!