Hi everyone,
I’m currently bussy with making a simple gathering script but I’m encountering the following:
The problem is when my unit enters the sphere collider, the gatherers amount has to be 1.
But when I test this and there is 1 unit entering, then the gatherers amount is 2.
Can someone explain me why this problem occurs?
Here is the code on my unit’s script:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
public class Unit : MonoBehaviour
{
public Harvest_Controller.ResourceTypes heldResourceType;
SelectUnit selectunit;
NavMeshAgent agent;
RaycastHit hit;
public Vector2 ScreenPos;
bool OnScreen = false;
public int HeldResource;
public int MaxHeldResource;
public bool isGathering = false;
void Start()
{
selectunit = GameObject.Find("World").GetComponent<SelectUnit>();
agent = this.gameObject.GetComponent<NavMeshAgent>();
StartCoroutine(GatherTick());
}
void Update()
{
ScreenPos = Camera.main.WorldToScreenPoint(this.transform.position);
if (selectunit.UnitWithinScreenSpace(ScreenPos))
{
OnScreen = true;
if (!selectunit.UnitsOnScreenSpace.Contains(this.gameObject))
selectunit.UnitsOnScreenSpace.Add(this.gameObject);
}
else
{
if (OnScreen)
{
selectunit.UnitsOnScreenSpace.Remove(this.gameObject);
OnScreen = false;
}
}
if (selectunit.selectedunit == this.gameObject || selectunit.selectedunits.Contains(this.gameObject))
{
if (Input.GetMouseButtonDown(1))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
if (hit.transform.tag == "Terrain")
{
agent.destination = hit.point;
}
else if (hit.collider.tag == "Resource")
{
agent.destination = hit.collider.gameObject.transform.position;
Debug.Log("Harvesting");
}
}
}
}
if (HeldResource >= MaxHeldResource)
{
//Drop off point here
}
}
public void OnTriggerEnter(Collider other)
{
GameObject hitObject = other.gameObject;
if (hitObject.tag == "Resource")
{
isGathering = true;
hitObject.GetComponent<Harvest_Controller>().gatherers++;
heldResourceType = hitObject.GetComponent<Harvest_Controller>().ResourceType;
}
}
private void OnTriggerExit(Collider other)
{
GameObject hitObject = other.gameObject;
if (hitObject.tag == "Resource")
{
isGathering = false;
hitObject.GetComponent<Harvest_Controller>().gatherers--;
}
}
IEnumerator GatherTick()
{
while (true)
{
yield return new WaitForSeconds(2);
if (isGathering)
{
HeldResource++;
}
}
}
}
And this is the code on my resource/harvestable:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Harvest_Controller : MonoBehaviour
{
private GameObject EnteredObject;
public enum ResourceTypes {Stone, Wood, Gold }
public ResourceTypes ResourceType;
public float harvestTime;
public int ResourceAmount = 1000;
public int waitSeconds = 2;
public int gatherers;
private void OnTriggerEnter(Collider collisionOther)
{
if (collisionOther.gameObject.tag == "SelectableUnit")
{
EnteredObject = collisionOther.gameObject;
EnteredObject.GetComponent<NavMeshAgent>();
StartCoroutine(ResourceTick());
}
}
// Update is called once per frame
void Update()
{
if (ResourceAmount <= 0)
{
Destroy(gameObject);
}
}
IEnumerator ResourceTick()
{
while (true)
{
//Every second is -1 ResourceAmount
yield return new WaitForSeconds(waitSeconds);
ResourceGather();
}
}
private void ResourceGather()
{
if (gatherers != 0)
{
ResourceAmount -= gatherers;
}
}
}