- Home /
Array without taged objects but without couting itself
Hi, I have done an array of gameobjects tagged as player, but I want to avoid to count himself, I mean, the object that cast this code it's a player too, and he make an array of enemies (of the other players), what I can do to exclude it from the list?
Here is the code for the array:
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Player");
Lot of thanks
Answer by Dreamblur · May 28, 2011 at 09:31 PM
Try the following:
EDIT:
var enemies = new Array(GameObject);
enemies = GameObject.FindGameObjectsWithTag("Player");
for(var i = 0; i < enemies.length; i++)
{
if(enemies[i] == this.gameObject)
{
enemies.splice(i, 1);
break;
}
}
Assets/Scripts/Player/ThirdPersonCharacterAttack.js(143,33): BCE0019: 'splice' is not a member of 'UnityEngine.GameObject[]'.
Sorry, I hadn't tested the code prior to posting it. Like iggy's solution, the splice method requires that you use an Array to store your game objects. I will edit my code to the tested, working version.
I have the problem that this list it's part of a code, and seems to work only with the list x__X''
you can convert it to normal Array, i reposted the clean solution. Its either that or using condition every time you iterate trough a list
Your answer
![](https://koobas.hobune.stream/wayback/20220612091239im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array Question Length (C#) 8 Answers
changing the tags in an array 1 Answer
List and store vector3 of game objects with tag 1 Answer
why does my code tag objects it's not supposed to? 0 Answers
instantiate a random object from multiples via 'tag' 1 Answer