Score counter not working properly.
Heya, I am new to Unity and c# ... I am just making a simple vertical shooter and am now up to keeping score! I just want to score 1 point each time an enemy is destroyed (all it takes is one shot from the bullet). Now at the moment, what my score counter is doing is scoring a point each time you hit the enemy, however if you miss the enemy, say 3 times in a row, the score will tally up for when you next hit the enemy.. So you could be on 1 point, miss three times, then hit an enemy and your score will go from 1 to 4 . I tried changing the score codes from the EnemyScript and putting it instead in the BulletScript, but no change. I think it's something simple I am missing but I am not sure what it is! Any help would be appreciated.. Here is my bullet script as it stands currently:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class BulletScript : MonoBehaviour {
public float BulletSpeed = 5f;
public PlayerController playerController;
public int Count;
public Text CountText;
void Update () {
transform.Translate (Vector3.up * BulletSpeed * Time.deltaTime);
}
void OnBecameInvisible () {
this.gameObject.SetActive (false);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
gameObject.SetActive (false);
playerController.CountText.text = playerController.Count.ToString();
}
void Start() {
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
}
}
Thank you!
So, it looks like you track your score in the "playerController.Count" variable? If that's the case, where is that variable incremented? It doesn't happen in the posted code.
Answer by TreyH · Feb 24, 2016 at 02:30 AM
playerController.Count++;
playerController.CountText.text = playerController.Count.ToString();
? You weren't incrementing anywhere here. :-)
Ah! Thank you so much. As I said I am quite new to this and after playing around with it for a while I completely forgot to add that part back in! Thank you :) :)
Your answer
Follow this Question
Related Questions
NEED HELP PLEASE! Unexpected symbol. 0 Answers
Bullet doesn`t die when enemy destory 1 Answer
Problem with Enemy AI 1 Answer