- Home /
Add transforms to Transform[] list at runtime
Essentially my game is a game where enemies have spawn points based on a Transform[] list. Well you have multiple paths you can take, so my old way of just replacing the list with every door no longer works. Is there a way I can add Transforms to this current list ? Example would be
Script 1 public Transform[] spawnPoints; public float SpawnTime; public GameObject Enemy; void Start(){
InvokeRepeating ("Spawn", SpawnTime, SpawnTime);
}
void Spawn ()
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (enemy, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
}
and Script 2 would have to work in a way that I can add to the list SpawnPoints. It's not as simple as
public Transform[] AddPoints;
public void AddSpawnPoints(){
SpawnPoints+=AddPoints;
}
I tried it in hopes , even tho I knew it wouldn't. So any help ?
Answer by Jessespike · Sep 15, 2015 at 07:07 PM
Try a list instead:
List<Transform> SpawnPoints;
SpawnPoints.AddRange( AddPoints );
I'll look into lists, never really used them before. I'll work it in and see how it goes. Thank you for the response
you'll need to include this to your script as well:
using System.Collections.Generic;
That explains why it was being weird lol. Saved me a search
Would you happen to know what I would replace int spawnPointIndex = Random.Range (0, spawnPoints.Length); with ? Because Length isn't something I can use, so I tried Get Range and got an error about floats. Would Capacity work ?
It works, thank you so much !!!!! Plus you introduced me to something awesome, as I've never known of Lists before. It's like a regular array but more flexible. Thank you