Hello, im new and still learning
Im trying do Enemies List Script, Enemy script assigned to all enemies and in script when my player attack, get list, search vs range and attack. But Enemies List all the time is clear
public class EnemyList : MonoBehaviour
{
static List<Enemy> Enemies = new List<Enemy>();
public List<Enemy> GetListOfEnemies()
{
return Enemies;
}
public void AddToList(Enemy enemy)
{
Enemies.Add(enemy);
}
public void RemoveFromList(Enemy enemy)
{
Enemies.Remove(enemy);
}
}
And Enemy Class
public class Enemy : MonoBehaviour
{
private void Awake()
{
GetComponent<EnemyList>().AddToList(this);
}
void Update()
{
if(GetComponent<Health>().IsDie())
{
GetComponent<EnemyList>().RemoveFromList(this);
}
}
}
and in player attack script i have something like this
[SerializeField] List<Enemy> enemies;
private void Awake()
{
enemies = GetComponent<EnemyList>().GetListOfEnemies();
}
where i have wrong way ?
Thanks guys
_
**This answer is generated by https://chat.openai.com/chat **
_
It looks like you’re trying to create a list of enemies that you can use to find nearby enemies and attack them. However, the way you’re doing this is not correct and won’t work as expected.
The problem with your code is that you’re using a static list to store the enemies. This means that there is only one list of enemies that is shared between all instances of the EnemyList class. This means that when you add an enemy to the list, it will be added to the same list that all other enemies are added to. This is not what you want.
Instead, you should create a new instance of the EnemyList class for each enemy, and add each enemy to its own list. This way, each enemy will have its own list of enemies that it can use to search for nearby enemies to attack.
To fix your code, you can remove the static keyword from the Enemies list, and create a new instance of the EnemyList class for each enemy. You can then add the enemy to its own list using the AddToList method. You can then use the GetListOfEnemies method to get the list of enemies for each enemy, and search for nearby enemies to attack.
Here is how your code could look like after making these changes:
Copy code
public class EnemyList : MonoBehaviour
{
// Remove the static keyword to make the list non-static
List<Enemy> Enemies = new List<Enemy>();
public List<Enemy> GetListOfEnemies()
{
return Enemies;
}
public void AddToList(Enemy enemy)
{
Enemies.Add(enemy);
}
public void RemoveFromList(Enemy enemy)
{
Enemies.Remove(enemy);
}
}
public class Enemy : MonoBehaviour
{
// Create a new instance of the EnemyList class
EnemyList enemyList = new EnemyList();
private void Awake()
{
// Add the enemy to its own list of enemies
enemyList.AddToList(this);
}
void Update()
{
if (GetComponent<Health>().IsDie())
{
// Remove the enemy from its own list of enemies
enemyList.RemoveFromList(this);
}
}
}
// In the player attack script
public class PlayerAttack : MonoBehaviour
{
[SerializeField] List<Enemy> enemies;
private void Awake()
{
// Get the list of enemies for the current enemy
enemies = GetComponent<EnemyList>().GetListOfEnemies();
}
}
I hope this helps! Let me know if you have any questions.
Thanks, this answer help me
But, before i dont add EnemyList script to Enemy, only to Player. Now all workly correct
You don’t need EnemyList
. This is how you do it:
public class Enemy : MonoBehaviour
{
public static List<Enemy> Instances = new List<Enemy>();
void OnEnable ()
{
Instances.Add( this );
}
void OnDisable ()
{
Instances.Remove( this );
}
}
And access i like this:
foreach( var enemy in Enemy.Instances )
{
// do something
}
_
Something for later time:
Once you get hang of it and came to a conclusion that iterating over all enemies results in spikes of low performance then I suggest you read into the subject of spatial hashing