Hello. It’s kind of a complicated problem, i don’t know if it’s my code’s fault or unity’s, i actually give up and ask the first time on a forum for unity help. First, here is the problem:
And here is the code needed for understanding:
IEnumerator StartQueue(Interactable currentInteractable)
{
xx = false;
nextInQueue = null;
agent.SetDestination(currentInteractable.interactPos);
yield return new WaitForSeconds(Time.deltaTime);
StartCoroutine(GetNextInQueue(currentInteractable, currentInteractable.lastInQueue));
while (xx == false && agent.remainingDistance > agent.stoppingDistance)
yield return new WaitForSeconds(Time.deltaTime);
StopCoroutine("GetNextInQueue");
nextInQueue = currentInteractable.lastInQueue;
currentInteractable.NextInQueue(this);
while (nextInQueue && !nextInQueue.finished)
{
agent.SetDestination(nextInQueue.transform.GetChild(0).position);
yield return new WaitForSeconds(Time.deltaTime);
}
agent.SetDestination(currentInteractable.interactPos);
yield return new WaitForSeconds(Time.deltaTime);
while (agent.remainingDistance > agent.stoppingDistance)
yield return new WaitForSeconds(Time.deltaTime);
arrived = true;
}
IEnumerator GetNextInQueue(Interactable currentInteractable, Customer lastInQueue)
{
while (lastInQueue == currentInteractable.lastInQueue)
yield return new WaitForSeconds(Time.deltaTime);
xx = true;
}
public class Interactable : MonoBehaviour
{
public Vector3 interactPos;
public Customer lastInQueue = null;
public void NextInQueue(Customer currentInQueue)
{
lastInQueue = currentInQueue;
}
public void FinishQueue(Customer currentInQueue)
{
Debug.Log(currentInQueue.name); // here is what is shown in the console
Debug.Log(lastInQueue.name); // (only the last 2 out of the total 4 lines matter, in the first 2 lines the customer goes to buy something, also it works very well there, the shelves inherit from the Interactable class too, and i use the StartQueue coroutine without problem, only on the register this weird thing happens)
if (currentInQueue == lastInQueue)
{
lastInQueue = null;
}
currentInQueue.finished = false;
}
}
public class Register : Interactable
{
void Start()
{
interactPos = transform.GetChild(0).position;
}
}
Basically, when FinishQueue is called, the if statement should be executed because lastInQueue is equal to currentInQueue, according to logic and the inspector, but somehow lastInQueue it’s null. I updated unity, the script register is not attached in multiple places, i don’t have multiple inspectors open. I don’t know what to do