Hello! Been looking at this for a little bit and have been wondering where I add a timer that delays the tree from despawning as the player is collecting wood from it? Here is the code:
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
public Vector3 targetPosition;
public int TotalWoodAmount;
public Text WoodCountText;
private int WoodCollect;
//private Rigidbody2D myRigidbody;
void Start()
{
//myRigidbody = GetComponent<Rigidbody2D>();
TotalWoodAmount = 0;
SetWoodCountText();
int minWood = 3;
int maxWood = 5;
WoodCollect = (Random.Range(minWood, maxWood));
}
// Update is called once per frame
void Update() {
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
transform.position = Vector2.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5);
//myRigidbody.velocity = Vector2.MoveTowards(myRigidbody.velocity, targetPosition, Time.deltaTime);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Tree"))
other.gameObject.SetActive(false);
TotalWoodAmount = TotalWoodAmount + WoodCollect;
SetWoodCountText();
}
void SetWoodCountText() {
WoodCountText.text = "Wood: " + TotalWoodAmount.ToString();
}
}
It’s probably something supremely obvious, I just can’t see it at the moment.
You disable the game object when you enter the trigger. If the collection is supposed to take some time (and let’s say for the sake of argument here, we don’t adapt if you walk away)… then you could start a coroutine that waits for a second or 2 before setting the game object to inactive and collecting your wood, and displaying the new score.
So in terms of a coroutine, it would work like the final example in the tutorial here:
right? However, I still kinda confused on where I should place and/or replace the code? Is it okay for you to show me what to change? By using “//” as I could ask you to do this for me, but I won’t learn anything from it. Hence why I am making this game. To learn how to use Unity 2D.
Let’s imagine that you really simplify your design, just to start.
- character walks to ‘tree’ (could be a square for all we care at the moment).
- trigger callback: check character/tree (depending where the script is), and if so begin “collection” code.
- collection code is a coroutine that waits for 2 secs, before giving resources/score.
- giving the resources/score (method) also adjusts the UI.
It’s not perfect, but that could be a baseline for you to start. If you can do that, you can probably work at making it more tailored for what you want.
I skimmed through that video, which seemed to be a good introduction to coroutines.
For your beginning example, I would say you can omit the moving part in the coroutine, and just do the collection/score/disable game object from it. Movement could be done as normal, and the interaction with a trigger, as mentioned.
Let me know if any particular portion of this post wasn’t clear. 
That does make sense so, for pseudocode purposes. I’d have probably an if statement that will check to see if the player’s hitbox and tree’s hitbox is colliding within the Update function. This code will also check the triggers are correct (maybe another if or while statement could be involved).
Then the coroutine code will have its own function (probably call it HarvestTime() ) which will then be added to the if statement and with the update of the UI afterwards. That seems like it makes sense? Right?
Sorry I like to visualize what the code looks like before I actually code and test it, plus having someone’s more experienced opinion will help me a lot. 
Colliders/Triggers are one option. If you use them, and your game objects are setup correctly then they will call OnCollisionEnter2D / OnTriggerEnter2D methods (if you write the appropriate one in your script). You do not need to check it in Update; you’d just check the valid gameobject/tag/component from what caused the trigger/collision. (trigger is generally easier for non-colliding things).
This is why some of the tutorials are really helpful. Nothing wrong with seeking help, but I’d mostly just be going over the fundamentals here, so far.
I’d say, getting started, just call the coroutine from the OnTriggerEnter2D. All the coroutine would do is wait, disable the object, and the other stuff.
The good thing about your goal here is it’s straight forward and broken down. You can try to finish piece by piece. If you get the collision working, move onto the next thing after… for example. 
Oh just to let you know, I figured how to do the delay/harvest timer just getting into a building menu and a way for the player to spend the resources gathered. Before fine tuning any sort of code is is so I have all the basics down before developing it all further. Thank you for your assistance, it was and still is greatly appreciated! 
Cool. Good luck with your game. 