Solved
List update
Basically I have public List<Transform> AvailableTargets;
which holds all buildings and player.
If the building is placed and then the enemy spawns it updates the list for that enemy. However if the enemy spawns first and then the building is placed it does not update it.
void Start()
{
AvailableTargets = new List<Transform>();
AddAllEnemies();
}
public void AddAllEnemies()
{
if (Faction == 1)
{
GameObject[] go1 = GameObject.FindGameObjectsWithTag("Building");
foreach (GameObject enemy in go1)
{
AddTarget(enemy.transform);
}
GameObject[] go2 = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject enemy in go2)
{
AddTarget(enemy.transform);
}
}
}
public void AddTarget(Transform enemy)
{
AvailableTargets.Add(enemy);
}
Then in update I call AddTarget function if new building is placed but it only updates for new enemies that spawn after it was placed.
Is there a way to update the list for enemies that are already in the scene?
If you only need one list perhaps you could simply make the list static?
This is working i think. However I have a new bug when i delete the building the transform is deleted and i get an error. Totally forgot about that.
Well you would somehow need to remove the target from the list when it gets destroyed. I must admit I am a bit confused about how you got everything arranged to give a good answer.
Follow this Question
Related Questions
Camera isn't move position? Why my camera isn't change position? 0 Answers
Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers
All GameObjects list to a GameObject? 0 Answers
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer
Problems With .rotate behavior 1 Answer