Hello,
I am working on an AI system in Unity, and I am encountering an issue during the target item collection process. When my AI character reaches a specific target item and collects the specified amount, the TextMesh Pro text under the Canvas updates, and a new target item count is generated. However, I am observing unexpected behavior during this process.
By the way I am abeginner in Unity:)
Situation:
- The AI character’s target is to collect 3 tomatoes.
- After the AI collects 3 tomatoes, the TextMesh Pro text updates to show 3/3.
- Subsequently, the text incorrectly updates to show 1/3 again.
- Due to this erroneous update, the AI removes the target from the list and does not proceed to collect items.
- The AI character does not move to collect the new item and remains idle.
Expected Behavior:
- The TextMesh Pro text should update correctly when the AI character collects the target item, and a new target should be assigned.
- The AI character should proceed to collect the new target item and remove the target point from its list.
Current Behavior:
- The TextMesh Pro text incorrectly updates to 1/3 after showing 3/3.
- The AI removes the target from its list and remains idle, not collecting any items.
What steps should I follow to resolve this issue, or what approach would you recommend? Thank you in advance for your assistance.
public void CollectNeededCount(Transform product, Enums.ProductType productType)
{
AIController thisAI = gameObject.GetComponent();
var currentYOffset = targetPoint.position.y;
if (productType == type)
{
collectedItems.Add(product);
aiUIController.CurrentItemCount(collectedItems.Count);
}
if (CurrentNeededCount > -1)
product.DOMove(targetPoint.position, 1f)
.SetEase(Ease.Linear)
.OnComplete(() =>
{
product.SetParent(transform);
product.localPosition = new Vector3(0, currentYOffset, 0);
currentYOffset += productSize;
if (CurrentNeededCount <= 0)
{
targetsToVisit.RemoveAt(0);
if (targetsToVisit.Count > 0)
{
collectedItems.Clear();
MoveTarget();
}
else if (targetsToVisit.Count == 0)
{
_checkoutInteractable.AddedAI(thisAI);
InternalEvents.FinishedAIProcessing.Invoke(gameObject.transform);
}
}
});
}
private List _aiControllerList = new List();
public AreaData_SO areaData;
public TriggeredAreaData dataTosend;
protected virtual void SendInteractableType()
{
InitilaizeDataToSend();
InternalEvents.PlayerTriggeredArea?.Invoke(dataTosend);
}
protected abstract void InitilaizeDataToSend();
protected virtual void EndInteractableType()
{
InternalEvents.PlayerOutArea?.Invoke(areaData.Area,areaData.ProductType);
}
public void Interact()
{
SendInteractableType();
}
protected void ShareProductWithAIList()
{
DOVirtual.DelayedCall(1.5f, DistributeProducts);
}
private void DistributeProducts()
{
if (dataTosend.salesList == null || dataTosend.salesList.Count == 0)
{
return;
}
while (dataTosend.salesList.Count > 0)
{
bool distributed = false;
for (int i = 0; i < _aiControllerList.Count; i++)
{
AIController aiController = _aiControllerList*;*
if (aiController.CurrentNeededCount > 0 && dataTosend.salesList.Count > 0)
{
Transform productToShare = dataTosend.salesList[0];
aiController.CollectNeededCount(productToShare, dataTosend.productType);
dataTosend.salesList.RemoveAt(0);
if (aiController.CurrentNeededCount == 0)
{
_aiControllerList.RemoveAt(i);
i–;
}
distributed = true;
}
}
if (!distributed)
{
break;
}
}
}
public virtual TriggeredAreaData AIInteract(AIController newAI)
{
if(!_aiControllerList.Contains(newAI))
_aiControllerList.Add(newAI);
return dataTosend;
}