public class Pine : MonoBehaviour
{
public float gatheringTime = 5f;
private int resourceInventoryAmount;
private float gatheringElapsedTimer;
private void Update()
{
if (gameObject.transform != null)
{
gatheringElapsedTimer += Time.deltaTime;
if (gatheringElapsedTimer >= gatheringTime)
{
resourceInventoryAmount--;
Collision.ReduceResourceAmount(resourceInventoryAmount);
Debug.Log("Amount: " + Collision.GetResourceAmount());
}
}
}
}
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…
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
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?:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class TreeResource
{
public static event EventHandler OnResourceAmountChanged;
private static int resourceAmount = 10;
public static void ReduceResourceAmount(int amount)
{
resourceAmount -= amount;
if (OnResourceAmountChanged != null) OnResourceAmountChanged(null, EventArgs.Empty);
if (resourceAmount <= 0)
{
//how the Tree object could be destroyed(?)
}
}
public static int GetResourceAmount()
{
return resourceAmount;
}
}
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…