- Home /
Question by
JackAshwell · May 30, 2020 at 01:21 PM ·
2dlist
Remove a game object from a list using an integer
So my code picks a random location from a list and a random enemy from the list and instantiates that, but now I want to remove that location from the list but I keep getting an error. My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public int currentLevel;
public List<GameObject> spawnPoints;
public int level1EnemyCount;
public int level2EnemyCount;
public int level3EnemyCount;
public int level4EnemyCount;
public int level5EnemyCount;
public int count;
public List<GameObject> enemies;
public void ActivateShop() {
SceneManager.UnloadSceneAsync("Main Game");
SceneManager.LoadScene("Shop");
}
void Start() {
level1();
}
void RandomSpawner() {
int randomEnemy = UnityEngine.Random.Range(0,enemies.Count);
int randomLocation = UnityEngine.Random.Range(0,spawnPoints.Count);
Instantiate(enemies[randomEnemy], spawnPoints[randomLocation].transform.position, transform.rotation);
spawnPoints.Remove(randomLocation);
}
void level1() {
if(count <= level1EnemyCount & currentLevel == 1) {
// Game is on level 1
for(count = 1; count <= level1EnemyCount; count++) {
Invoke("RandomSpawner", 1f);
}
}
}
}
I know it's to do with
spawnPoints.Remove(randomLocation);
But I don't know exactly how to fix it.
Thanks!
Comment
Best Answer
Answer by KoenigX3 · May 30, 2020 at 01:34 PM
List.Remove(T item) needs a parameter with the type of T. That is the type you declared the list with. If you want to remove an item from a list with an index, you can use List.RemoveAt(int index).
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
I want to illuminate 2d sprites in Unity 2D. 0 Answers
2D GameObject Array to 2D List 1 Answer