Score Reseting

  1. My Score is not going up by the set amount
  2. 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;
}	

}

public void Score(int Points) {
	Debug.Log("Scored Points");
	mytext.text = pointss.ToString();
	pointss += Points;
}	

It looks like you’ve got your statements in the wrong order. This updates the score text then updates the points variable. It would make more sense to do it the other way around otherwise the points for a kill won’t update until the next time you call this.

void Start(){
    mytext = GetComponent();`
}

There’s no type here to get. I think this should be GetComponent<Text>(); I’d also set pointss =0 and mytext.text = "0" here just to make sure your variables are defaulting correctly.

public void Reset () {
     mytext.text = pointss.ToString();
}

I don’t know what you’re trying to do with this. It would just output the points to the string? I’d suggest just removing this as it doesn’t seem to be useful and as stated in the start function, resetting the variables there.

Privacy & Terms