Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Instantiate ground transform x units from previous ground prefab

Discussion in 'Scripting' started by footballhono52, Dec 15, 2015.

  1. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    Im having a problem trying to figure out an algorithm and how to code getting my ground prefabs to instantiate in a logical order where they dont overlap each other. To start off this is just a prototype infinite runner and the object is to get the ground prefabs to instantiate randomly while the runner runs infinitely to the right. The one code that works is the code from the unity infinite runner tutorial (37:45) but with my ground prefabs they overlap each other. I've tried changing the values testing multiple values and it doesn't seem to change much and when it does its unplayable with the platforms spread too far apart.

    I tried modifying the code to spread them farther apart but it actually doesn't compile. Its a nullreference error but im just completely stuck on it. I'll provide the code that ran but overlapped below too. I attacked a picture of the overlapping


    Code that I modified
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnScript : MonoBehaviour {
    5.  
    6.     public GameObject[] obj;
    7.     private Transform transform;
    8.     public float spawnMin = 1f;
    9.     public float spawnMax = 2f;
    10.     public float wait = 1f;
    11.  
    12.     IEnumerator Spawn(Collider2D other)
    13.     {
    14.         Instantiate (obj [Random.Range (0, obj.Length)], transform.position, Quaternion.identity);
    15.         if(other.CompareTag("Ground"))
    16.         {
    17.             yield return new WaitForSeconds(wait);
    18.             Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
    19.         }
    20.         else
    21.             Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
    22.     }
    23.  
    24.     void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.         StartCoroutine (Spawn (other));
    27.     }
    28. }
    Code that ran
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnScript : MonoBehaviour {
    6.  
    7.     public GameObject[] obj;
    8.     public float spawnMin = 1f;
    9.     public float spawnMax = 2f;
    10.  
    11.     void Start()
    12.     {
    13.         Spawn ();
    14.     }
    15.  
    16.     void Spawn()
    17.     {
    18.         Instantiate (obj [Random.Range (0, obj.Length)], transform.position, Quaternion.identity);
    19.         Invoke ("Spawn", Random.Range(spawnMin, spawnMax));
    20.     }
    21. }
    22.  
     

    Attached Files:

  2. OrSi333

    OrSi333

    Joined:
    Jan 5, 2014
    Posts:
    5
    Are the objects only created left-to-right? (AKA can I rely on the fact that the new object will HAVE to be on the right of the previous object?)
     
  3. footballhono52

    footballhono52

    Joined:
    Dec 3, 2015
    Posts:
    16
    yes they are only created on the right side of the screen and move left. I have a OutOfBoundsDestroyer that destroys the prefab ground piece shortly after the camera passes