Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Instantiating Prefab Consecutively Set Distance Apart.

Discussion in 'Scripting' started by Zohresh, Apr 25, 2022.

  1. Zohresh


    Apr 12, 2014

    I'm working on an endless runner. I'm having an issue instantiating prefabs at a set distance apart, while my level is being generated based on 3 sections (each section is the same prefab instantiated by a list), as the player object crosses the trigger at the end of a section, the last section in line is moved to the front of the list.

    I've tried to instantiate from an object's position both attempted an object inside the prefab and outside the prefab. The problem I come across is that the way I'm spawning the object that holds the consecutive spawners "spawnPosition" moves each time the Instantiation is called by the for loop. If I decide to do what is currently set each time the trigger is crossed the objects continuously spawn further and further out away from the player until the player is no longer coming in contact with the objects.

    The SpawnLaneTrigger() method is called each time the player crosses a trigger in the middle of a section mentioned above.

    I see what the problem is and why it's happening, but I don't know what a good solution to the problem is. I've been googling like a mad man trying to come to a good solution, but I've reached what I believe is the edge of my knowledge at this time to remediate my current problem.

    Help would be greatly appreciated!

    Code (CSharp):
    1. public class LaneSpawner : MonoBehaviour
    2. {
    3.     [SerializeField] private int maxLaneCount;
    4.     [SerializeField] private GameObject spawnPosition;
    6.     public GameObject lanes;
    7.     private Vector3 currentPosition = new Vector3(0, 1, 60);
    8.     private Vector3 offset = new Vector3(0, 0, 8);
    9.     private int distance = 3;
    15.     public void Start()
    16.     {
    17.         spawnPosition.transform.position = new Vector3(0, 0, 0) + spawnPosition.transform.position;
    19.     }
    25.     public void SpawnLaneTrigger()
    26.     {
    27.         for (int i = 0; i < maxLaneCount; i++)
    28.         {
    29.             SpawnLanes();
    30.         }
    31.     }
    33.     public void SpawnLanes()
    34.     {
    36.         GameObject.Instantiate(lanes,currentPosition + offset, Quaternion.identity);
    37.         currentPosition.z += distance;
    39.         /*
    40.    {
    41.        GameObject ln = GameObject.Instantiate(lanes, spawnPosition + offset, Quaternion.identity);
    42.        spawnPosition.transform.position = ln.transform.position;
    43.    }
    44.   */
    45.     }
    46. }
  2. Kurt-Dekker


    Mar 16, 2013
    Sounds like you need to break things apart.

    - decide where to start spawning

    - store that position in a Vector3

    - for each spawn:

    ----> spawn the object
    ----> move that Vector3 forward by the proper amount
    ----> wait until it's time to spawn the next object