Start Function instantiate infinite number of prefabs?

Hey Guys,

I have created a prefab . I wanted to instantiate this prefab just once. To do this I wrote the line of code in Start function. But Prefabs is being instantiated like I wrote in Update function. I couldn’t solve why.

Here are my codes :

  1. [CreateAssetMenu(menuName = "Create An Enemy")]
  2. public class EnemyConfig : ScriptableObject
  3. {
  4. // It shows which enemy is it
  5. public GameObject enemyPrefab;
  6. }
  7. ``
  8. ``
  9. ``
  10. public class FollowThePath : MonoBehaviour
  11. {
  12. [SerializeField]
  13. EnemyConfig enemyConfig;
  14. private void Start()
  15. {
  16. Instantiate(enemyConfig.enemyPrefab, enemyConfig.enemyPathPrefab[0].position ,Quaternion.identity );
  17. }

Hi Alpay,

Could it be that the instantiated game object has got a FollowThePath script as well? If so, its Start method gets called, and another game object of this type gets instantiated. That would result in an endless loop.


See also:

Thanks. It really helped a lot.

So, Is this because of prefab ?

Because We have done this before but the gameobject wasn’t prefab

That does not have anything to do with prefabs or game objects. If the instantiated object has got the FollowThePath script attached, the Start method gets executed and instantiates another object of that type.

This topic was automatically closed after 4 hours. New replies are no longer allowed.

Privacy & Terms