- Home /
Why won't my score counter count normally? sometimes it counts and other times it does nothing. Can someone help?
[CODE] using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class balldestroy : MonoBehaviour {
static int count;
public Text countText;
// Use this for initialization
void Start () {
count = 0;
SetCountText ();
Destroy (this.gameObject, 10);
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
if(Input.GetMouseButton (0))
{
count += 1;
SetCountText ();
Destroy (this.gameObject);
}
}
void SetCountText(){
if(countText != null){
countText.text = "" + count.ToString ();
}
}
} [\CODE]
Answer by MattG54321 · Jul 24, 2017 at 02:27 PM
When you call Destroy (this.gameObject)
, your script won't run anymore because the object it's attached to has been destroyed.
Try removing both Destroy (this.gameObject, 10);
and Destroy (this.gameObject);
. If it works, you'll have to find another way to keep score or remove the ball so your script can keep counting the score.
If the problem persists, you'll know that the cause is somewhere else, and we'll have to look deeper.
I'm purely guessing here but looking at the script I'd say this same script is attached to multiple objects that shoud be destroyable by clicking.
count
is static so the score should increment O$$anonymous$$ with each click.
Your answer
Follow this Question
Related Questions
Scoring/Counting how many specific objects are placed on a specific plane. 1 Answer
Resouce script help 3 Answers
GuiText Score On Mouse Click 1 Answer
Roguelike Elements... 1 Answer
Counter problem 1 Answer