Question by
saifalethawi · Oct 05, 2018 at 06:12 PM ·
debuggingif statement
not sure why if statement not coming true! :'(
hey guys!
I picked up unity and C# 3 days ago! trying to learn as much as possible!
I've been stuck on this for a few hours now, i am following this tutorial to learn: Link
I've created a "TaskList.cs" script that includes the "states" of units:
public enum TaskList {
Gathering,
Moving,
Idle,
Building,
Attacking,
Delivering
}
and I have a code for the statements to change depending on the input a player gives:
public class ObjectInfo : MonoBehaviour {
public TaskList task;
public ResourceManager RM;
public NodeManager.ResourceTypes heldResourceType;
// bools return false statements
private bool moving = false;
public bool isSelected = false;
public bool isGathering = false;
private bool mRunning = false;
public string objectName;
private NavMeshAgent agent;
private Animator mAnimator;
public int heldResource;
public int maxHeldResource;
public GameObject[] drops;
GameObject targetNode;
// Use this for initialization
void Start () {
StartCoroutine(GatherTick());
agent = GetComponent<NavMeshAgent>();
mAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(targetNode == null)
{
if(heldResource != 0)
{
drops = GameObject.FindGameObjectsWithTag("Drops");
agent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}else
{
task = TaskList.Idle;
}
}
if(heldResource == maxHeldResource && isGathering)
{
isGathering = false;
Debug.Log("Inventory is full! " + heldResource + "/" + maxHeldResource);
//resources drop off
drops = GameObject.FindGameObjectsWithTag("Drops");
agent.destination = GetClosestDropOff(drops).transform.position;
drops = null;
task = TaskList.Delivering;
}
if (Input.GetMouseButtonDown(1) && isSelected)
{
RightClick();
}
if (agent.remainingDistance <= agent.stoppingDistance)
{
mRunning = false;
}
else
{
mRunning = true;
}
if (agent.remainingDistance <= agent.stoppingDistance && moving)
{
Debug.Log("Stopped!");
moving = false;
}
mAnimator.SetBool("running", mRunning);
}
GameObject GetClosestDropOff(GameObject[] dropOffs)
{
GameObject closestDrop = null;
float closestDistance = Mathf.Infinity;
Vector3 position = transform.position;
foreach(GameObject targetDrop in dropOffs)
{
Vector3 direction = targetDrop.transform.position - position;
float distance = direction.sqrMagnitude;
if(distance < closestDistance)
{
closestDistance = distance;
closestDrop = targetDrop;
}
}
return closestDrop;
}
public void RightClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
if(hit.collider.tag == "Ground")
{
agent.destination = hit.point;
moving = true;
Debug.Log("Moving...");
task = TaskList.Moving;
}else if(hit.collider.tag == "Resource")
{
agent.destination = hit.collider.gameObject.transform.position;
Debug.Log("Harvesting...");
task = TaskList.Gathering;
}
}
}
public void OnTriggerEnter(Collider other)
{
GameObject hitObject = other.gameObject;
if(hitObject.tag == "Resource" && task == TaskList.Gathering)
{
isGathering = true;
hitObject.GetComponent<NodeManager>().gatherers++;
heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
targetNode = hitObject;
}else if(hitObject.tag == "Drops" && task == TaskList.Delivering)
{
if (RM.iron >= RM.maxIron)
{
task = TaskList.Idle;
}else
{
RM.iron += heldResource;
heldResource = 0;
task = TaskList.Gathering;
agent.destination = targetNode.transform.position;
}
}
}
public void OnTriggerExit(Collider other)
{
GameObject hitObject = other.gameObject;
if(hitObject.tag == "Resource")
{
hitObject.GetComponent<NodeManager>().gatherers--;
isGathering = false;
}
}
IEnumerator GatherTick()
{
while (true)
{
yield return new WaitForSeconds(1);
if (isGathering)
{
heldResource++;
}
}
}
}
everything worked fine before I implemented the "TaskList". the way it was set up is: there was a "trigger zone" where if a mouse input was detected inside the area, isGathering would be set to true!
but when i added the "TaskList" it did not work anymore, the "Task" wouldn't change at all and would stay "idle"
thanks in advance guys!
Comment