My Instantiated enemies do not add there score to the score counter. why?
I am creating a 2D Asteroids game but I seem to be having trouble getting my score counter to work. I am very new too programming and I do not even know how to explain what I am trying to fix. My goal is to spawn enemies that when are hit by my bullets they are destroyed from the scene and a number of points should be added to my score counter. It works fine for the first asteroid that spawns. The score goes up by 1 point then if a UFO spawns next and I hit it with a bullet it is destroyed also but the UFO points that should be added are 10 but the score reads 10 when it should read 11 like as if it resets and then adds the 10 points. Then if I hit another Asteroid after hitting a UFO it reads 1 point again on the score counter. Then if I hit another UFO after the Asteroid it reads 10 again but if I hit a UFO after another UFO the score still say 10 when it should read 20 and if I hit an asteroid after another asteroid the score stays at 1 when it should read 2. I know there is something very simple wrong with my scripts but I am totally confused and if I remove the destroy(game object) from each script when I keep hitting the same UFO the score rises by ten points the way I want it too but the UFO is still on the scene obviously and that is not what I want. I want it to be deleted from the scene at this point. The same with the asteroid if I remove Destroy(gameObject) from its script the score rises by 1 but only on the first asteroid and UFO the others do not add any points but if I hit them they still still read add point in the console window as I am using Debug.Log to test everything. The first script below is my Destroy on collision class it is just holding some attributes that will be inherited from my other scripts
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class DestroyOnCollision : MonoBehaviour { public Text Score; public int count;
public void SetScoreText()
{
Score.text = "Score: " + count.ToString();
}
}
Below is my Set Score On Start function that inherits the destroy on collision classes attributes.
public class SetScoreOnStart : DestroyOnCollision
{ void Awake() { count = count + 0; SetScoreText(); }
}
And then I have two scripts one for each enemy to handle when points are added and how many points are added also they handle how and when my enemies will be destroyed from the scene.
Asteroid Destroy Script
using UnityEngine; using System.Collections;
public class DestroyAsteroid : DestroyOnCollision {
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Bullet")
{
count = count + 1;
SetScoreText();
Debug.Log("Add Point!");
Destroy(gameObject);
Debug.Log("Destroy Asteroid!");
}
}
}
Destroy UFO Script
using UnityEngine; using System.Collections;
public class DestroyUFO : DestroyOnCollision {
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Bullet")
{
count = count + 10;
SetScoreText();
Debug.Log("Add Point!");
Destroy(gameObject);
Debug.Log("Destroy UFO!");
}
}
}
Any help would be appreciated as this is the last thing I need to fix to complete the game I am very new to c# so please do not make fun if I sound like a noob because I am. I hope you guys can understand what I am trying to achieve and the way I have explained everything if you need more information please let me know.