[2D][C#] Need help with AI

Hi everyone!

I still working on my bots. On this case I have the problem that the bots after destroy his target, it stop move.

I explain: The bots must move in the map from one target to another (like way points)and when they touch one target, it must disappear. In this way, the targets act like “food”, and when a bot touch the “food”, it disappear and the bot going to the next one, but the problem come when the bot set a target and “devour” it, the bot stop move and the editor give me a error saying that the transform has been destroyed.

I don’t know if I explained well, but I want this:

-That when the bot “devour” a target, the bot go to the next one.
-Each time that I start the play mode, the bot go to a certain target, while all targets have the same tag. If it’s not too much to ask, how I can make that the bot go to a random target each time that I start the play mode?

I’m trying to make this for two months and I cannot do it, that is why I ask to the community. Thanks.

There is my bot script:

using UnityEngine;
using System.Collections;

public class bot_profile : MonoBehaviour
{
    public string Name = "botname";
    public string Aggressivity;                              // Bot Difficulty | passive | normal | hostile |
    public float BotMoveSpeed;                               // Bot movement speed
    public float BotRotationSpeed;
    public static float BotCurrentMass = 10f;                // Bot mass
    public static float BotCellSize = 0.5f;
    public float MaxDistance;

    private GameObject bbody;
    public Transform food_target;
    private Transform myTransform;

    void Awake()
    {
        myTransform = transform;
    }

    void Start()
    {
        bbody = GameObject.FindGameObjectWithTag("bot");
        food_target = GameObject.FindWithTag("food").transform;
    }

    void Update()
    {
        bbody.transform.localScale = new Vector3(BotCellSize, BotCellSize, BotCellSize);

        if (Aggressivity == "passive")
        {
            Vector3 dir = food_target.position - myTransform.position;

            dir.z = 0.0f;

            if (dir != Vector3.zero)
            {
                myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.FromToRotation(Vector3.right, dir), BotRotationSpeed * Time.deltaTime);
            }

            myTransform.position += (food_target.position - myTransform.position).normalized * BotMoveSpeed * Time.deltaTime;
        }
    }

    void OnTriggerEnter2D(Collider2D tfood)
    {
        if(tfood.gameObject.tag == "food")
        {
            BotCurrentMass += 1;
            BotCellSize += 0.015f;
            Destroy(tfood.gameObject);
        }
    }
}

You are not finding the next piece of food…

Try this…

    void Update()
    {
        bbody.transform.localScale = new Vector3(BotCellSize, BotCellSize, BotCellSize);
        if(food_target == null){
            food_target = GameObject.FindWithTag("food").transform;
        }
        if(food_target == null) return;

 
        if (Aggressivity == "passive")
        {
            Vector3 dir = food_target.position - myTransform.position;
 
            dir.z = 0.0f;
 
            if (dir != Vector3.zero)
            {
                myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.FromToRotation(Vector3.right, dir), BotRotationSpeed * Time.deltaTime);
            }
 
            myTransform.position += (food_target.position - myTransform.position).normalized * BotMoveSpeed * Time.deltaTime;
        }
    }

also

Wow Thanks, it work! but… are there any form to do that the bots set a random target?, because all the bots go to the same target, so each one will going to a different direction. Thanks.

You could make a static controller that manages that. it would spawn food and then when the bot eats some food it would ask for another. You would also have to make sure that any food eaten was no longer in the list to choose from.