how to RemoveAt

I’m (still) a beginner and i was wasting hours to figure out how to use it. so i thought i give a basic example of it for any other beginners who are searching here in “Answers”

to be edited: at the moderators: i hope it will be ok if i answer my own question.

first of all ; make sure that , using System.Collections.Generic; is at top !
make 2 or more different primitives, place them where everyou want, scale and rename them.
Tag them, in my case i used 2 Tags: Enemy + Obstacles

here’s my examplescript in which i placed Game Objects with different tags in 1 List!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FindOthers : MonoBehaviour {

	public GameObject[] Obstacles; // Obstacles = the name of this Game Object array
	public GameObject[] Enemies;
	public List<Transform> AllOthers;

	public GameObject thisobj;

	// Use this for initialization
	void Start () 

	void FixedUpdate () 
	void FindObstacles()
		Obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
		Enemies = GameObject.FindGameObjectsWithTag("Enemy");

	void UpDateList_AllOthers()
		// declaring AllOthers as new List is necessary !!!
		AllOthers = new List<Transform>(); // wichtig !! um Liste bearbeiten zu können !

		foreach(GameObject enemy in Enemies)// enemy is the name of each game object in the Game Object array
			// damit wird ausgeschlossen,daß dieses Enemy Objekt in AllOthers Liste aufgenommen wird !!
			// i use this because i will attach the script to any npc enemy and so 
			// this prevents that this public game object (= thisobj) is added 

			Vector3 dist = thisobj.transform.position - enemy.transform.position;
			// so dist.sqrMagn. isnt really necessary but enemy.activeSelf
			if( dist.sqrMagnitude > 0.0f && enemy.activeSelf)
				AllOthers.Add(enemy.transform); // this adds all objects tagged as "Enemy"

		foreach(GameObject obstacle in Obstacles)// obstacle is the name of each game object in the Game Object array
				AllOthers.Add(obstacle.transform); // this adds all objects tagged as "Obstacle"

		foreach(Transform tr_allothers in AllOthers) // tr_allothers is the name for all Transforms added to the List
			// the position of all transforms of this list in world space
			Vector3 AllOtherspos = new Vector3( tr_allothers.position.x, tr_allothers.position.y, tr_allothers.position.z);

			Debug.Log("pos = " + AllOtherspos + " " + "scale = " + tr_allothers.localScale);

			if(tr_allothers == null)
				AllOthers.RemoveAt(tr_allothers.GetSiblingIndex());// this removes this transform

attach the script to an empty game object or to an enemyobject. in playmode activate/deactivate the objects. you should see that any object which is deactivated disappears off the list and if reactivated it’s in the list again.

hope this is helpfully and it works like it should ( for me its working fine!)
if so and/or you like it as well if anyone has some more advanced suggestions leave me a comment.


I went on and figured out how to get the index of each object in the List AllOthers;
therefore i’ve added 4 new variables

private Vector3 Abstand;        //Abstand = Distance  
public float min_Abst = 5.0f;  //is kind of a sensor with a range of 5.0f  
                                                    //(depends on  your game objectssize)  
private int myindex;
private int GetSiblingIndex;  
//i changed the public game object: thisobj  
//to public Transform = thistransform !  

first of all : leave some space between each object because detecting if they are closer than min_Abstand or even overlapping isnt part of the script up to now.

right after the foreach(Transform tr_allothers in AllOthers) loop in the UpDateList_AllOthers() function i’ve added :

		for (int i = 0; i <= AllOthers.Count-1;i++)
			GetSiblingIndex = i;
			Abstand = thistransform.position - AllOthers*.position;*
  •  	if(Abstand.magnitude <= min_Abst)*
  •  	{*
  •  		Debug.Log("Warning ! " +"pos = "+ i+ " = "+Abstand);*
  •  	}*
  •  }*