Search Unity

{C#} spawning a list of objects in order and not at randaom

Discussion in 'Scripting' started by OneCluelessGuy, Oct 30, 2014.

  1. OneCluelessGuy

    OneCluelessGuy

    Joined:
    Oct 30, 2014
    Posts:
    1
    Hi, I am trying to create a list of objects to be spawn right in front of the character. Almost like an infinite runner but I don't want it to be infinite, I wan to control the prefabs so I can load the "finish line" at the end and not at random.

    I managed to create a script that spawns my prefabs and recycles them when not in view (by following some tutorials online). I have no idea how to change the code to make it so its not random anymore. I have tried to mess with it but I only make things worse.

    I am attaching the script. Any help would be appreciated.

    Thanks for your time :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class PlatformManager : MonoBehaviour {
    5.    
    6.     public Transform[] platformlist;
    7.     public int numberOfObjects;
    8.     public float recycleOffset;
    9.     public Vector3 minSize, maxSize, minGap; //maxGap;
    10.     private Vector3 nextPosition;
    11.     private Queue<Transform> objectQueue;
    12.    
    13.  
    14.     void Start () {
    15.         GameEventManager.GameStart += GameStart;
    16.         GameEventManager.GameOver += GameOver;
    17.        
    18.         objectQueue = new Queue<Transform>(numberOfObjects);
    19.         for(int i = 0; i < numberOfObjects; i++){
    20.             objectQueue.Enqueue((Transform)Instantiate(platformlist[Random.Range(0, platformlist.Length)]));
    21.         }
    22.         nextPosition = transform.localPosition;
    23.         for(int i = 0; i < numberOfObjects; i++){
    24.             Recycle();
    25.         }
    26.     }
    27.  
    28.     void Update () {
    29.         if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
    30.             Recycle();
    31.         }
    32.     }
    33.  
    34.     private void Recycle () {
    35.         Vector3 scale = new Vector3(
    36.             Random.Range(minSize.x, maxSize.x),
    37.             Random.Range(minSize.y, maxSize.y),
    38.             Random.Range(minSize.z, maxSize.z));
    39.        
    40.         Vector3 position = nextPosition;
    41.         position.x += scale.x * 0;
    42.         //position.y += scale.y * 0f;
    43.        
    44.         Transform o = objectQueue.Dequeue();
    45.         o.localScale = scale;
    46.         o.localPosition = position;
    47.         objectQueue.Enqueue(o);
    48.        
    49.        
    50.         nextPosition += new Vector3(
    51.             Random.Range(minGap.x, minGap.x),
    52.             //Random.Range(minGap.y, maxGap.y),
    53.             Random.Range(minGap.z, minGap.z));
    54.  
    55.         //if(nextPosition.y < minY){
    56.             //nextPosition.y = minY + maxGap.y;
    57.         //}
    58.         //else if(nextPosition.y > maxY){
    59.             //nextPosition.y = maxY - maxGap.y;
    60.         //}
    61.  
    62.     }
    63.    
    64.     private void GameStart () {
    65.         nextPosition = transform.localPosition;
    66.         for(int i = 0; i < numberOfObjects; i++){
    67.             Recycle();
    68.         }
    69.         enabled = true;
    70.     }
    71.    
    72.     private void GameOver () {
    73.         enabled = true;
    74.     }
    75. }