Question by
BillyCoen · Jul 05, 2019 at 10:56 PM ·
instantiateprefabs
Instantiate a prefab at positions on a list
Im using Tilemap to map out available tiles that the enemies can spawn on. Right now im trying to instantiate a decided number of enemies on those tiles at random locations. I cant find much of anything on what im trying to do. and when i start the game it doesnt spawn enemies.
Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class enemyspawn : MonoBehaviour
{
public Tilemap EnemyMap;
public GameObject Enemy;
public int n;
public int numberEnemies;
public List<Vector3> availablePlaces;
public List<GameObject> Goomba;
void Start()
{
EnemyMap = transform.GetComponentInParent<Tilemap>();
availablePlaces = new List<Vector3>();
for (int n = EnemyMap.cellBounds.xMin; n < EnemyMap.cellBounds.xMax; n++)
{
for (int p = EnemyMap.cellBounds.yMin; p < EnemyMap.cellBounds.yMax; p++)
{
Vector3Int localPlace = (new Vector3Int(n, p, (int)EnemyMap.transform.position.y));
Vector3 place = EnemyMap.CellToWorld(localPlace);
if (EnemyMap.HasTile(localPlace))
{
//Tile at "place"
availablePlaces.Add(place);
}
else
{
return; //No tile at "place"
}
}
}
EnemySpawn();
}
void EnemySpawn()
{
{
for (int i = 0; i < n; i++)
{
if (availablePlaces.Count == 0) return;
int index = Random.Range(0, availablePlaces.Count);
Vector3 spawnPosition = availablePlaces[index];
Instantiate(Goomba[Random.Range(0, Goomba.Count)], spawnPosition, transform.rotation);
//Remove the position from list what was used
availablePlaces.RemoveAt(index);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Create gameobject in other scene... 0 Answers
Instantiation of my GameObjects spell (from other script) 0 Answers
How to edit new clone properties? 1 Answer
problems with instantiating a moving prefab. 0 Answers
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer