How to create scripts that help gathering resources ? (With code)

Hello friends, first of all im rookie here ! I have lots of question about gathering resources with AI. Let me talk about what i did. My scenario is one or more cow reach food gameobject and gain energy while collide with it. When they start gaining energy, i want decrease food gameobject and when it contain 0 food__ __it must be destroyed. When i run this script there is no change.

I want ;

  1. Decrease containedfood
    2)Increase cow energy when collide
  2. Stop cow when collide ( I need an idea how to stop when collide )
public class Food : MonoBehaviour
{

    Cow _cow;
    public float containedFood = 100f;
    public float currentFood;
//----------------------------------------------------------------
    void Start()
    {
        currentFood = containedFood;
    }
    //----------------------------------------------------------------
    private void Awake()
    {
        _cow = GameObject.FindObjectOfType<Cow>();
    }
    //----------------------------------------------------------------
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Cow")
            _cow.StopCow(); // I want stop cow when enter collision and play eating anim in this section.
    }
    //----------------------------------------------------------------
    void OnCollisionStay(Collision collision)
    {

        if (collision.gameObject.name == "Cow")
        {
            if (currentFood > 1 && currentFood < 100)
            {
                _cow.GainEnergy();
                currentFood -= 1;
            }

            if (currentFood == 0)
                Destroy(gameObject);

            print(containedFood);
        }
    }
}

If you use energy component on the Cow, then you can get that component, and update energy inside OnCollisionStay.

1 Like

Several notes:

_cow = GameObject.FindObjectOfType<Cow>(); careful with FindObjectOfType because it will return the first thing it can find. If you have multiple game objects in the scene with the Cow script attached then it will create a bug.

collision.gameObject.name == "Cow" when you rename the game object this will fail.
You can either use a tag or use GetComponent<Cow>(); and do a null check on that.
OnCollisionEnter → add the cow reference (if you have multiple cows you’d need a List<Cow>), set eating animation
OnCollisionStay → process the cow reference (or if you have multiple cows you can foreach through the List<Cow>), use a timed decrease of current food and increase energy of the cow. why timed? because OnCollisionStay happens every frame. food will decrease based on framerate. which is not something you want. You can make it time based using a timer. if there are cows eating { every X seconds → distribute food }
OnCollisionExit → remove the cow reference, stop eating animation

2 Likes

thanks let me see what can i do :slight_smile:

if (currentFood > 1 && currentFood < 100)
I think this will not work, because your currentFood is set to 100 at start.

1 Like

oh i forgot delete this part when tried to paste here ^^.