Distance based object generation.

Greetings all.

I’ve been chipping away on a little 2D endless runner game over the past 2 weeks between work and struggling to get a firm foothold on a script I am trying to write. I have an object generator that spawns pooled objects (Gen_A) and I would like to implement another object generator with pooled objects (Gen_B). I am trying to achieve a result that will deactivate Gen_A when the player has travelled a certain distance which in turn will activate Gen_B. I have tried a number of approaches with little to no success. I appreciate if any light could shed on the subject in order to help point me in the right direction.

Thanks.

Hello,
Could you please show you object generating code?
Please don’t forget to include you code in code tags :slight_smile:

Sure thing. I should note that the Asteroid Generator is a child of the camera.

using UnityEngine;
using System.Collections;

public class AsteroidGenerator : MonoBehaviour {

    public GameObject theAsteroid;
    public Transform generationPoint;
    public float distanceBetween;

    private float asteroidWidth;

    public float distanceBetweenMin;
    public float distanceBetweenMax;

    // public GameObject[] theAsteroids;
    private int asteroidSelector;
    private float[] asteroidWidths;

    public ObjectPooler[] theObjectPools;

    private float minHeight;
    public Transform maxHeightPoint;
    private float maxHeight;
    public float maxHeightChange;
    private float heightChange;

    // Use this for initialization
    void Start ()
    {
        // asteroidWidth = theAsteroid.GetComponent<BoxCollider2D>().size.x;

        asteroidWidths = new float[theObjectPools.Length];

        for (int i = 0; i < theObjectPools.Length; i++)
        {
            asteroidWidths[i] = theObjectPools[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
        }

        minHeight = transform.position.y;
        maxHeight = maxHeightPoint.position.y;
    }
  
    // Update is called once per frame
    void Update ()
    {

        if(transform.position.x < generationPoint.position.x)
        {
            distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);

            asteroidSelector = Random.Range(0, theObjectPools.Length);

            heightChange = transform.position.y + Random.Range(maxHeightChange, -maxHeightChange);

            if(heightChange > maxHeight)
            {
                heightChange = maxHeight;
            }

            else if (heightChange < minHeight)
            {
                heightChange = minHeight;
            }

            transform.position = new Vector3(transform.position.x + (asteroidWidths[asteroidSelector] / 2) + distanceBetween, heightChange, transform.position.z);

 

            // Instantiate (/* theAsteroid */ theAsteroids[asteroidSelector], transform.position, transform.rotation);

            GameObject newAsteroid = theObjectPools[asteroidSelector].GetPooledObject();

            newAsteroid.transform.position = transform.position;
            newAsteroid.transform.rotation = transform.rotation;
            newAsteroid.SetActive (true);

            transform.position = new Vector3(transform.position.x + (asteroidWidths[asteroidSelector] / 2), transform.position.y, transform.position.z);

        }
  
    }
}