problem with a list that has to store different things for multiple gameobjects

i have two gameobject with both a task_list that they can follow but when i add a task to one of the gameobjects it gets also added to the other one how could i prevent that (they both use the same list but i want them to store different tasks into them)

    public class PriorityList : MonoBehaviour
    {
    
        public string CurrentTask = "";
        public Vector3 CurrentTaskLocation;
        public List<string> TaskList;
        public List<Vector3> TaskLocation;
        public float timeLeft = 15.0f;
        private TaskObjects taskObjects;
        private NavMeshAG navMeshAG;
    
        private void Start()
        {
    
            navMeshAG = gameObject.GetComponent<NavMeshAG>();
        }
        void Update()
        {
            if (timeLeft <= 1 && TaskList.Count == 0 && TaskLocation.Count == 0)
            {
                CurrentTask = "";
                timeLeft = 1f;
            }
            if (CurrentTask == "Vullen")
            {
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 3f;
                }
            }
            if (CurrentTask == "klanthelpen")
            {
                //times will differ from costemer to custemer
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 0.5f;
                }
            }
            if (CurrentTask == "splitsen")
            {
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 1.5f;
                }
            }
            if (CurrentTask == "Pauze")
            {
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 3.5f;
                }
            }
            if (CurrentTask == "kassa")
            {
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 3f;
                }
            }
            if (CurrentTask == "spiegelen")
            {
                
                timeLeft -= Time.deltaTime;
                if (timeLeft <= 0)
                {
                    CurrentTask = TaskList[0];
                    TaskList.RemoveAt(0);
                    CurrentTaskLocation = TaskLocation[0];
                    TaskLocation.RemoveAt(0);
                    timeLeft = 60f * 2f;
                }
            }
        }
    
    }

I think what you are doing is putting the list on a game object in the hierarchy and then adding it to the public variables in the inspector of the 2 objects that require the list. If this is correct. You are using the same instance of the list for both game objects that are referencing it.


To avoid this behaviour, you need to have a separate instance of the list for each object. Just add the list as a script/component on your object in the inspector.


If you for some reason need to ensure that the list only has one instance, you would have to make custom logic within the list to ensure the correct game objects have access to the correct items, but this seems a bit silly.