Progressbar not working properly on click.

So I have a basic progress bar that runs in my woodcutting script. When I click on the tree that its attached to it set to setactive(true), and once its destroyed it goes back to setactive(false). However when I run the SetActive(false) in start it allows me to click on one tree, but not the other. When I take out SetActive(false) in start, the progress bar shows at start up, when I click on tree it works, then it hides the bar, and then when I click on the second tree it does the same thing(which is how its supposed to be). What am I doing wrong here? I basically want the progressbar to not show till I click on the tree, and hide when its done.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Woodcutting : MonoBehaviour

{

    Inventory inventory;
    public float timeRemaining = 10f;
    private bool counterActive = false;
    public Transform LoadingBar;
    [SerializeField]
    private float currentAmount;
    [SerializeField]
    private float speed;
    GameObject progressBar;
    GameObject loadingBar;


    // Use this for initialization
    void Start()
    {

        inventory = GameObject.Find("Inventory").GetComponent<Inventory>();
        progressBar = GameObject.Find("ProgressBar");       
        progressBar.SetActive(false);
    }

    void OnMouseDown()
    {
        counterActive = true;
    }

    void Update()
    {

        if (timeRemaining > 0 && counterActive == true)
        {
            progressBar.SetActive(true);
            Debug.Log("Waitting..." + timeRemaining);
            currentAmount -= speed * Time.deltaTime;
            LoadingBar.GetComponent<Image>().fillAmount = currentAmount / 100;
            timeRemaining -= Time.deltaTime;
            if (timeRemaining <= 0)
            {
                inventory.AddItem(0);
                progressBar.SetActive(false);
                Destroy(gameObject);
            }
        }
    }
}

GameObject.Find only returns active objects so any tree after the first to initialise isn’t going to manage to find it as it will have been set to false by then.

I would suggest having the progress bar held as a public variable in another script on one object, like a game manager and then having each tree fetch it from this object at start with GetComponent. Then it shouldn’t matter if it is active or not.

EDIT (in response to your comment):

Declare it the same way you’re already doing with the LoadingBar in this script.

Your game manager object would have something like this on it:

using UnityEngine;
using System.Collections;

public class GameManagerScript: MonoBehaviour {

             public GameObject progressBar;
    
             void Start()
             {
             progressBar.SetActive(false);
             }    
    }

Drag the progress bar object to that variable in the inspector.

Your tree script may then want to do something like this:

     private GameObject gameManager;

     void Start()
     {
         inventory = GameObject.Find("Inventory").GetComponent<Inventory>();
         gameManager = GameObject.Find("GameManager");
         treeProgressBar = gameManager.GetComponent<GameManagerScript>().progressBar;    
     }

 void OnMouseDown()
 {
     treeProgressBar.SetActive(true);
 }

That should work and enable the trees to call the progress bar even if it has been deactivated. Obviously in the long run it is going to be better to avoid using GameObject.Find as much as possible so you might want to roll the inventory script into the game manager or use a more efficient method to get the object.

Okay I’m back @Fredex8 , this may be an easy fix and I’m being dumb. So basically I have a basic script that allows my progress bar to follow my character. Here is my issue http://i.imgur.com/qltHDuC.png .

My script for it to follow my player. Any idea?

public class ProFollow : MonoBehaviour
{

    public RectTransform canvasRectT;
    public RectTransform healthBar;
    public Transform objectToFollow;
    void Update()
    {
        Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, objectToFollow.position);
        healthBar.anchoredPosition = screenPoint - canvasRectT.sizeDelta / 2f;
    }
}