Search Unity

Infinite Runner Floor Creation

Discussion in 'Scripting' started by bdutton12, May 18, 2019.

  1. bdutton12

    bdutton12

    Joined:
    Apr 11, 2019
    Posts:
    3
    I have floor that spawns and destroys on collision with a destroyer but I use a list to translate each floor object and it seems that the new items aren't being added to the list properly? Here are the 2 scripts controlling this:

    Code (CSharp):
    1. public class FloorMovement : MonoBehaviour
    2. {
    3.     public GameObject[] tiles;
    4.     public List<GameObject> ts;
    5.     public int speed;
    6.     public GameObject[] obstacles;
    7.     GameObject obsInQues;
    8.     private void Start()
    9.     {
    10.         GameObject floor1 = Instantiate(obstacles[0], new Vector3(0, 0, 0), Quaternion.identity);
    11.         ts.Add(floor1);
    12.         GameObject floor2 = Instantiate(obstacles[0], new Vector3(0, 0, 60), Quaternion.identity);
    13.         ts.Add(floor2);
    14.         GameObject floor3 = Instantiate(obstacles[0], new Vector3(0, 0, 120), Quaternion.identity);
    15.         ts.Add(floor3);
    16.         GameObject floor4 = Instantiate(obstacles[0], new Vector3(0, 0, 180), Quaternion.identity);
    17.         ts.Add(floor4);
    18.     }
    19.     private void Update()
    20.     {
    21.         moveFloor();
    22.     }
    23.     public void moveFloor()
    24.     {
    25.         tiles = ts.ToArray();
    26.         for (int i = 0; i <= tiles.Length; i++)
    27.         {
    28.             GameObject tile = tiles[i];
    29.             tile.transform.Translate(0, 0, -speed);
    30.         }
    31.     }
    32.     public void SpawnObstacle()
    33.     {
    34.         int obs = Random.Range(0, obstacles.Length);
    35.         GameObject currentObs = Instantiate(obstacles[obs], new Vector3(0, 0, 240), Quaternion.identity);
    36.         ts.Add(currentObs);
    37.         Debug.Log("floor piece added");
    38.     }
    39. }
    40.  
    Code (CSharp):
    1. public class Destroyer : MonoBehaviour
    2. {
    3.     private void OnCollisionEnter(Collision collision)
    4.     {
    5.         FloorMovement removeFloorFromList = FindObjectOfType<FloorMovement>();
    6.         removeFloorFromList.ts.Remove(collision.gameObject);
    7.         Debug.Log("Collision");
    8.         Destroy(collision.gameObject);
    9.         Create();
    10.     }
    11.  
    12.     private void Create()
    13.     {
    14.         FloorMovement CreateObs = FindObjectOfType<FloorMovement>();
    15.         CreateObs.SpawnObstacle();
    16.     }
    17. }
     
    Last edited: May 18, 2019
  2. bdutton12

    bdutton12

    Joined:
    Apr 11, 2019
    Posts:
    3
    I solved it but idk how to mark it as solved here, could someone help lmao thanks