- Home /
The question is answered, right answer was accepted
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;
}
}
}
}
If each of your gameobject has a script on it which has a task_list then this should not be the case. Can you show some code, that'll make it easier to see what is going wrong.
this is the navigation script: public class Nav$$anonymous$$eshAG : $$anonymous$$onoBehaviour { public Nav$$anonymous$$eshAgent agent; public Vector3 startPossision; public Vector3 TargetPosition; public bool alreadyInlist = false; public Nav$$anonymous$$eshAG nav$$anonymous$$esh; public PriorityList priorityList;
int layer_mask;
// Start is called before the first frame update
void Start()
{
layer_mask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Vloer");
nav$$anonymous$$esh = gameObject.GetComponent<Nav$$anonymous$$eshAG>();
priorityList = gameObject.GetComponent<PriorityList>();
agent = gameObject.GetComponent<Nav$$anonymous$$eshAgent>();
nav$$anonymous$$esh.enabled = true;
}
// Update is called once per frame
public void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(2))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit/*, layer_mask*/))
{
TargetPosition = hit.point;
Debug.Log(hit.point);
if (agent.remainingDistance <= 1)
{
nav$$anonymous$$esh.enabled = true;
}
if(hit.collider.gameObject.TryGetComponent(typeof(TaskObjects), out Component component))
{
Debug.Log("found it in" + hit.collider.name);
if(priorityList.CurrentTask == null || priorityList.CurrentTask == "")
{
priorityList.CurrentTask = hit.collider.gameObject.GetComponent<TaskObjects>().What_Am_I_Used_For;
}
else
{
//contain the task and the location of the task in Script(priorityList)
priorityList.TaskList.Add(hit.collider.gameObject.GetComponent<TaskObjects>().What_Am_I_Used_For);
}
if (priorityList.CurrentTaskLocation == null || priorityList.CurrentTaskLocation == Vector3.zero)
{
priorityList.CurrentTaskLocation = hit.collider.gameObject.GetComponent<TaskObjects>().gameObject.transform.position;
}
else
{
//contain the task and the location of the task in Script(priorityList)
priorityList.TaskLocation.Add(hit.collider.gameObject.GetComponent<TaskObjects>().gameObject.transform.position);
}
}
}
//agent.SetDestination(startPossision + new Vector3(1, 0, 0));
agent.SetDestination(priorityList.CurrentTaskLocation);
}
}
}
Answer by highpockets · Apr 28, 2020 at 09:02 PM
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.