Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by mkcuk, Nov 28, 2019.

  1. mkcuk

    mkcuk

    Joined:
    Oct 11, 2019
    Posts:
    11
    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
    3) Stop cow when collide ( I need an idea how to stop when collide )



    Code (CSharp):
    1. public class Food : MonoBehaviour
    2. {
    3.  
    4.     Cow _cow;
    5.     public float containedFood = 100f;
    6.     public float currentFood;
    7. //----------------------------------------------------------------
    8.     void Start()
    9.     {
    10.         currentFood = containedFood;
    11.     }
    12.     //----------------------------------------------------------------
    13.     private void Awake()
    14.     {
    15.         _cow = GameObject.FindObjectOfType<Cow>();
    16.     }
    17.     //----------------------------------------------------------------
    18.     void OnCollisionEnter(Collision collision)
    19.     {
    20.         if (collision.gameObject.name == "Cow")
    21.             _cow.StopCow(); // I want stop cow when enter collision and play eating anim in this section.
    22.     }
    23.     //----------------------------------------------------------------
    24.     void OnCollisionStay(Collision collision)
    25.     {
    26.  
    27.         if (collision.gameObject.name == "Cow")
    28.         {
    29.             if (currentFood > 1 && currentFood < 100)
    30.             {
    31.                 _cow.GainEnergy();
    32.                 currentFood -= 1;
    33.             }
    34.  
    35.             if (currentFood == 0)
    36.                 Destroy(gameObject);
    37.  
    38.             print(containedFood);
    39.         }
    40.     }
    41. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    If you use energy component on the Cow, then you can get that component, and update energy inside OnCollisionStay.
     
    mkcuk likes this.
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,064
    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
     
    Last edited: Nov 28, 2019
    mkcuk and GoliathAT like this.
  4. mkcuk

    mkcuk

    Joined:
    Oct 11, 2019
    Posts:
    11
    thanks let me see what can i do :)
     
  5. VSM2018

    VSM2018

    Joined:
    Jun 14, 2018
    Posts:
    16
    if (currentFood > 1 && currentFood < 100)
    I think this will not work, because your currentFood is set to 100 at start.
     
    mkcuk likes this.
  6. mkcuk

    mkcuk

    Joined:
    Oct 11, 2019
    Posts:
    11
    oh i forgot delete this part when tried to paste here ^^.