Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set 3D animation as Legacy(Debug Menu Grayed Out!) 0 Answers

Modify 'Copy PDB Files' via code ? 1 Answer

MySQL Connector : Runtime Error issue 0 Answers

Trouble Attaching Unity Editor to Process for MonoDevelop debugging'... 1 Answer

If an integer ends with an 1. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges