e.g. a missle prefab for each enemy. Each prefab has some attributes different. So that when each enemy fire a missle, it can instantiate its own missle prefab.
can I copy the missle prefab for each enemy with code, then change some of its attributes, without instantiating it in the scene, at or before the start of game?
There are ways to spawn a prefab with out physically putting it in the scene. One way is to set the prefab to a variable in the script
public class prefabspawn :MonoBehaviour
{
public GameObject prefab;//click&drag direct form Project window.
// Use this for initialization
void Update ()
{
if(spawnCondition)
{
Instantiate(prefab,Vector3.zero, Quaternion.identity);
}
}
But, performance wise, from what I understand calling Instantiate or Destroy can be bad if you have to do it “in the middle of the action” So in this case it may be better to spawn the prefabs you are going to need when you first load up the scene (or have them in there already?). If you look at the AngryBots project that ships with Unity you will see this project does something like this. Particularly in the Spawner.js code you will see that they create a cache of bullet and missles when the game first loads and then when other object try to spawn the missles or bullets the Spawner code, instead of instantiating a new bullet or missle, will go through the cache it has already created and activate one of the elements that is currently inactive. Also when it destroys it really only inactivates the object and makes it available in the cache again. It will only actually instantiate if there isn’t an available cache object.