Search Unity

Question How do I end reducing resource when worker leaves

Discussion in 'Scripting' started by Unitkiv, Feb 11, 2024.

  1. Unitkiv

    Unitkiv

    Joined:
    Jun 14, 2022
    Posts:
    71
    This is the script in my pine tree component:
    Code (CSharp):
    1. public class Pine : MonoBehaviour
    2. {
    3.     public float gatheringTime = 5f;
    4.  
    5.     private int resourceInventoryAmount;
    6.     private float gatheringElapsedTimer;
    7.  
    8.     private void Update()
    9.     {
    10.         if (gameObject.transform != null)
    11.         {
    12.             gatheringElapsedTimer += Time.deltaTime;
    13.  
    14.             if (gatheringElapsedTimer >= gatheringTime)
    15.             {
    16.  
    17.                 resourceInventoryAmount--;
    18.                 Collision.ReduceResourceAmount(resourceInventoryAmount);
    19.  
    20.                 Debug.Log("Amount: " + Collision.GetResourceAmount());
    21.             }
    22.         }
    23.     }
    24. }
    The worker gets in it is fine but when he leaves the resource, I see on the Console Tab the reduction process doesn't end. I need some idea please...
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,709
    this reads a little confusing.. So i assume resourceinventoryamount, is the amount available on the tree? Somewhere i assume the worker has a script that says "you gained pine" ... realistically these should be joined.. so when tree looses a point, player gains it. However, you need to gather controlled by the worker, eg, it starts it, and it stops it. Looking at the above, nothing stops that gather.. nothing..so you could set 5 as the amount and collect 500
     
    Bunny83 and Unitkiv like this.
  3. Unitkiv

    Unitkiv

    Joined:
    Jun 14, 2022
    Posts:
    71
    Yes, resourceInventoryAmount is effecting the tree. I put the gather controller on worker. Now the counting down is working. Actually, I store the resourceInventoryAmount in a TreeResource script which I didn't attach to the tree. thus how do I destroy the tree when the resourceAmount falls to 0?:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEngine;
    6. public class TreeResource
    7. {
    8.     public static event EventHandler OnResourceAmountChanged;
    9.     private static int resourceAmount = 10;
    10.  
    11.     public static void ReduceResourceAmount(int amount)
    12.     {
    13.         resourceAmount -= amount;
    14.         if (OnResourceAmountChanged != null) OnResourceAmountChanged(null, EventArgs.Empty);
    15.         if (resourceAmount <= 0)
    16.         {
    17.             //how the Tree object could be destroyed(?)
    18.         }
    19.     }
    20.  
    21.     public static int GetResourceAmount()
    22.     {
    23.         return resourceAmount;
    24.     }
    25. }
     
    Last edited: Feb 11, 2024
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,709
    That doesnt make sense either

    so, lets say you set resourceInventoryAmount to 10

    you then call the `Collision.ReduceResourceAmount(resourceInventoryAmount);` in your Pine with 9 .. and the tree resource reduces by 9... (Collision.Reduce... cant be this tree resource surely? what Collison? sounds like somewhere you overwrote the unity collision class)

    Next time it reduces the treeresource counter by 8 ... then by 7..

    Your code feels really disorganised.