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;
}
}
Answer by Paricus · Dec 04, 2016 at 08:32 PM
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've already try to make your instruction, but it got an error is occurred, maybe I did something wrong, would you $$anonymous$$d to tell me more in deep about detail. Sorry for my beginner skill guys
could you submit you error? I don't write the scripts in a compiler, so it may just be syntax errors
you need to establish the wincanvas variable
public GameObject wincanvas;
you should't give up, its all part of learning to program
How could I send my full script cause of some syntax may error. I have one script of this problem on my target game object
Ok, I'm not completely sure what you already have in the project, but i'll write scripts as if you're just starting the project.
Count Script Create an empty game object and attach this script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : $$anonymous$$onoBehaviour {
public GameObject winText;
int enemyCount = 3;
public void checkEnemiesRemaining()
{
enemyCount --;
if(enemyCount == 0)
winText.SetActive(true);
}
Target Script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
public ScoreCounter scoreScript; //make sure to drag empty game object (hierarchy view) with the ScoreCounter script into here (inspector view)
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ball")
{
GameObject effect = Instantiate(prefabEffect, transform.position, prefabEffect.transform.rotation) as GameObject;
scoreScript.SetCountText()
Handheld.Vibrate();
Destroy(gameObject);
}
}
}
Answer by JeffHardddyyy · Dec 04, 2016 at 04:48 PM
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.
}
I use your SetCountText() method and it gets the same result
Answer by kayonkykt · Dec 06, 2016 at 12:59 PM
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;
}
are you not getting an error in the console view
Your answer
![](https://koobas.hobune.stream/wayback/20220612101932im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mobile bloom kills performance 1 Answer
Black Material when camera rotate 1 Answer
Shadows problem on mobile 1 Answer
MouseUp, MouseEnter and MouseExit work on android? 2 Answers
how to make unlock/lock sytem for player selection? 0 Answers