How to solve "Target left counting"

The following code is about counting target left ,
it can count on first time but the rest is not(3 targets)
maybe something went wrong
*I’m a beginner for this

void OnTriggerEnter(Collider other)
{

	if (other.gameObject.tag == "Ball")
    {
        GameObject effect = Instantiate(prefabEffect) as GameObject;
        effect.transform.position = this.transform.position;
		Destroy(gameObject);
        Handheld.Vibrate();
		
		targetTotal -= 1;
		targetText.text = "Target left: " + targetTotal;

		if (targetTotal == 0)
			winText.enabled = true;

    }

   
   
}

you’re destroying the script that holds the target total variable, you should create a new script that doesn’t get destroyed and use that to hold if (targetTotal == 0)

 public CounterScript counterScript;

 if (other.gameObject.tag == "Ball")
 {
     GameObject effect = Instantiate(prefabEffect) as GameObject;
     effect.transform.position = this.transform.position;
     counterScript.TargetTotal();
     Handheld.Vibrate();
     Destroy(gameObject);
    }
 }

Counter Script

public class CounterScript:MonoBehavior
{
      int targetTotal = <value>

     public void TargetTotal()
        {
              targetTotal -= 1;
              targetText.text = "Target Left: " + targetTotal

             if(targetTotal == 0)
                  winText.enable = true;
        }
}

Add an empty game object to the scene and attach the counter script, then drag that game object onto the game object that holds your script, in the inspector window.

Hope this helped

I presume you are doing the Roll-A-Ball Course,

So props on that. try this :

/*Please know that I just copied and pasted it from when I did the Roll-A-Ball course, I did explain it. Take the things that you need from it, I did explain it for myself and I do have a disability so if it doesn't make sense, please reply, I will go more in depth. I did the entire script so if you are like "What is this for" you can copy that selection, then if you go "Oh, I may need this too!" I allowed that as well. Good luck! If you reply with a comment, I will try my best to answer, Good luck in Unity dude!*/


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

public class PlayerController : MonoBehaviour {

	public float speed;
	public Text countText;
	//The text that will be counting
	public Text winText;
	//The text that will display when you get 'x' points

	private Rigidbody rb;
	private int count;

	void Start ()
	{
		rb = GetComponent<Rigidbody>();
		count = 0;
		SetCountText ();
		winText.text = "";
	}
	//Sets the count text to 0. Change "Count" to however many you want them to start with.

	void FixedUpdate ()
	{
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

		rb.AddForce (movement * speed);
	}
	// This part of the script includes the movement.

	void OnTriggerEnter(Collider other) 
	{
		if (other.gameObject.CompareTag ( "Pick Up"))
		{
			other.gameObject.SetActive (false);
			count = count + 1;
			SetCountText ();
		}
	}
	//If anything touches anything with the tag "Pick Up" it will +1 the count.

	void SetCountText ()
	{
		countText.text = "Count: " + count.ToString ();
		if (count >= 12)
		{
			winText.text = "You Win!";
		}
	}
	// Says the count, and if the count is greater than / equal to, It will say "You Win!"
	// Change the "12" to assign the points until you want them to win.
}

Try this:

void Update () { // The code you posted }

Error again, this is my target script

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

public class goal : MonoBehaviour
{

public GameObject prefabEffect;
public AudioClip shoot;
private AudioSource audioPlayer;

public Text targetText;
public Canvas wincanvas;
int enemyCount = 3 ;
public ScoreCounter scoreScript;

// Use this for initialization
void Start ()
{

	targetText.text = "Target left: " + enemyCount;
	audioPlayer = this.GetComponent<AudioSource>();

	wincanvas.enabled = false;

}

 void OnTriggerEnter(Collider other)
{
	if (other.gameObject.tag == "Ball")
    {
        GameObject effect = Instantiate(prefabEffect) as GameObject;
        effect.transform.position = this.transform.position;

		audioPlayer.PlayOneShot (shoot);

		scoreScript.checkEnemiesRemaining ();

		Destroy(gameObject);

        Handheld.Vibrate();

    }
		  
}

// Update is called once per frame
void Update ()
{
    this.transform.localRotation =
        Quaternion.Euler(0, 1, 0) * this.transform.localRotation;

}

}

and it’s your target script

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

public class ScoreCounter : MonoBehaviour {
public Canvas wincanvas;
public Text targetText;
int enemyCount = 3 ;

public void checkEnemiesRemaining()
{
	enemyCount--;
	targetText.text = "Target left: " + enemyCount;
	if(enemyCount == 0)
		wincanvas.enabled = true;
}

@Paricus