- My Score is not going up by the set amount
- My Score is reseting every once and a while
Scripts
Enemy Script =
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public float projectilespeed;
public GameObject playerprojectile;
public float health = 150;
public float shotspersecond = 0.5f;
public int ScoreValue = 250;
private TextController scorekeeper;
void Start() {
scorekeeper = GameObject.Find("Score").GetComponent<TextController>();
}
void OnTriggerEnter2D (Collider2D col) {
ProjectileScript missile = col.gameObject.GetComponent<ProjectileScript>();
if (missile) {
health -= missile.GetDamage();
missile.Hit();
if (health <= 0) {
Destroy(gameObject);
scorekeeper.Score(ScoreValue);
}
Debug.Log("Hit by a projectile");
}
}
void Fire() {
Vector3 startposition = transform.position + new Vector3(0,-1,0);
GameObject missile = Instantiate(playerprojectile,startposition,Quaternion.identity) as GameObject;
missile.rigidbody2D.velocity = new Vector2(0,-projectilespeed);
}
void Update() {
float probabillity = shotspersecond * Time.deltaTime;
if (Random.value < probabillity) {
Fire ();
}
}
}
ScoreScript =
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController : MonoBehaviour {
public int pointss;
private Text mytext;
// Use this for initialization
public void Reset () {
mytext.text = pointss.ToString();
}
void Start(){
mytext = GetComponent();
}
// Update is called once per frame
public void Score(int Points) {
Debug.Log("Scored Points");
mytext.text = pointss.ToString();
pointss += Points;
}
}