Add trasform to list using editor gui.

I have a set of transform objects with tag = “waypoint” and i have a enemy gameobject.

Now when ever i duplicate a waypoint transform in editor it should automatically add to the enemy script which has a list of transform.

Is this achievable through editor scripting.

I am new to editor scripting and using this code.

public class EnemyTest : MonoBehaviour {

	public List<Transform> wayPointList;
}



[CustomEditor(typeof(EnemyTest))]
public class EnemyPathEditor : Editor {

	public override void OnInspectorGUI (){

		DrawDefaultInspector();

		EnemyTest enemy = target as EnemyTest;
		GameObject [] wayPoint = GameObject.FindGameObjectsWithTag("Waypoint");

		for(int i=0; i<wayPoint.Length; i++){
			GameObject wayPointGameObj = EditorGUILayout.ObjectField(wayPoint*, typeof(GameObject), true) as GameObject;*
  •  	enemy.wayPointList.Add(wayPointGameObj.transform);*
    
  •  }*
    
  • }*
    }
    Now whenever i am clicking on enemy the waypoint transforms are getting added continuously.
    First i want it to add only the given amount of waypoints which is there in the scene.
    Second i want the list to add waypoint automatically whenever i duplicate a gameobject.

For the Looping issue :

using UnityEngine;
using System.Collections.Generic;

[ExecuteInEditMode]
public class EnemyTest : MonoBehaviour {

    [HideInInspector]
    public List<Transform> wayPointList = new List<Transform>();

    public void Start(){
        GameObject[] t = GameObject.FindGameObjectsWithTag("Waypoint");
        for (int i = 0; i < t.Length; i++)
        {
            wayPointList.Add(t*.transform);*

}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

[CustomEditor(typeof(EnemyTest))]
public class EnemyPathEditor : Editor {

EnemyTest enemy;
Transform wayPointGameObj;

public void OnEnable(){
enemy = (EnemyTest)target;
}

public override void OnInspectorGUI (){

DrawDefaultInspector();

for (int i = 0; i < enemy.wayPointList.Count; i++)
{
EditorGUILayout.BeginHorizontal();
wayPointGameObj = EditorGUILayout.ObjectField(enemy.wayPointList*, typeof(Transform), true) as Transform;*
if(GUILayout.Button(“-”)){
enemy.wayPointList.Remove(enemy.wayPointList*);*
}
EditorGUILayout.EndHorizontal();
/*EditorGUILayout.BeginHorizontal();
if(GUILayout.Button(“0”)){
enemy.wayPointList*.position = Vector3.zero;*
}
enemy.wayPointList_.position = EditorGUILayout.Vector3Field(“”,enemy.wayPointList*.position);_
_EditorGUILayout.EndHorizontal();/
}
}
}
I added a " - " button to remove any unnecessary Transform.
I don’t think you can check for duplicates, but I’ll double check it. If I find something I’ll Update the answer._