- Home /
list.contains isn`t working
hello everyone, im trying to create a minimap, and for it i need an array that contains all the objects i want in the minimap (in this case the planets). Im using the following code:
foreach(GameObject item in GameObject.FindGameObjectsWithTag("Planet")){
if (Vector3.Distance(gameObject.transform.position, item.transform.position) < dist &&
markers.Contains(item) == false){
markers.Contains(item) == false){
GameObject go = new GameObject ("Marker_" + item.name);
go.transform.parent = transform;
go.transform.localPosition = item.transform.position/dist;
markers.Add(go);
}
the problem is when i execute the code it adds the items in the sceene every frame. i need it to be dinamic, you know in case an enemy is near. any siggestions are much apreciated, thanks in advanced
Answer by Linus · Nov 07, 2014 at 02:39 AM
Forgot if on line 5:
if( markers.Contains(item) == false){
I would not recommend to do GameObject.FindGameObjectsWithTag("Planet") on every frame. But instead keep a manager that holds them, and have the object register to it OnEnable and unregister on OnDisable
Your answer
Follow this Question
Related Questions
why does "new GameObject" creates 2 emptys 1 Answer
C# List For Loop Only Returns Last Element 0 Answers
A node in a childnode? 1 Answer
Using Loops 1 Answer
"for" loop - DrawTexture problem. 0 Answers