0
\$\begingroup\$

I'm working on a game in which obstacles are Instantiated randomly 5 to 20 units ahead of the player.

When they are instantiated, I want to add them to a list so that when there are too many obstacles loaded in the game, it will remove them from the list and then delete them from the game.

I'm having trouble adding them to the list, because using obstacleList.Add(Obstacles.name); doesn't work.

Here's my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Generate : MonoBehaviour{

    //player position
    public Transform Player;

    // The prefab for an obstacle
    public GameObject Obstacle;

    // Holds float distance from 0 for each obstacle that is instantiated
    private float nextObstacle = 30;

    // is supposed to hold the names of all active instances of the obstacle prefab
    List<string> obstacleList = new List<string>(10);

    void FixedUpdate()
    {
        //Checks to see if the player is colser than 30 units to the most recently spawned obstacle
        if (nextObstacle - Player.position.x < 30)
        {
            //Generates a new obstacle
            GenerateObstacle();
        }
    }

    void GenerateObstacle()
    {
        // Randomly assignes a number for horizontal and vertical Transform
        int randH = Random.Range(5, 20);
        int randV = Random.Range(1, 3);

        //Instantiates an obstacle using those random values
        Instantiate(Obstacle, new Vector3(nextObstacle + randH, randV, 0), Quaternion.identity);

        //Makes that obstacle the new distance so a new obstacle can be spawned
        nextObstacle += randH;

        // Is supposed to add the name of the obstacle that was Instantiated to a list
        obstacleList.Add(Obstacle.name);

        //checks when list has more than 10 items in it
        if (obstacleList.Count > 10)
        {
            DeleteObstacles();
            // Removes the first item from the list
            obstacleList.RemoveAt(0);
        }
    }
    void DeleteObstacles()
    {
        // Supposed to destroy the copied prefab
        Destroy(Obstacle);
    }
}
\$\endgroup\$
1
\$\begingroup\$

A List<string> is all well and good if you just want names. But you don't want to delete the name, you want to delete the game object.

Also, if you always want to delete from the start of the collection (First-In, First-Out), and you don't need to traverse or randomly access items in the collection, then the structure you want is a Queue.

So, replace:

List<string> obstacleList = new List<string>(10);

with:

Queue<GameObject> obstacles = new Queue<GameObject>(10);

Next, capture the newly-created instance when you make it:

var instance = Instantiate(Obstacle, new Vector3(nextObstacle + randH, randV, 0), Quaternion.identity);

And add it to your queue:

obstacles.Enqueue(instance);

Your removal can then look like this (put it before you add so you don't exceed your promised capacity):

if (obstacles.Count >= 10)
{
    var toBeDestroyed = obstacles.Dequeue();
    Destroy(toBeDestroyed);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.