Need Help making a Kill counter

Well I am Working on a project for School, right now I am making a game where you are a cube that shoots smaller cubes that come out of spawners. When the smaller cubes hit you they cause me to loose a life which I have displayed. now, In order to make the game havea goal i want to base winning the levels on the number of the enemy cubes that I have destroyed. So what I need help with is making a Gui that will display how many kills I have gotten and when I get to a certain number of kills win the level and load a new level. right now I have a script that will display “SCORE: 0” but when I fire the lazer it appears to place another copy of the score gui on top of the first onebelow is the script i am using… i am am still very new to C# scripting so simplicity is key.

here is my code

using UnityEngine;
using System.Collections;

public class KillCounter : MonoBehaviour {

public int Score = 0;

public void OnGUI() {
GUI.Label(new Rect(20, 10, 100, 20), System.String.Format(“SCORE: {0}”, Score));
}

public void OnTriggerEnter(Collider collider) {
if (collider.gameObject.tag == “Enemy”)
Score++;

}
}

Heres an idea, post the script that you have for firing the laser.

using UnityEngine;
using System.Collections;

public class LaserBlast : MonoBehaviour {

public GameObject LaserPrefab;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Mouse0)) {
Instantiate(LaserPrefab, transform.position, Quaternion.identity);
}
}
}

As i can see, you put GUI script on a laser prefab, so, when you instantiate new copy of laser - you also create a copy of GUI function, and each of them draw label, until disabled or destroyed. Put OnGUI and Score in your master player script. Also, i recommend you to try static values for Score.

In Player script:

static public Score: int;

void OnGUI() {
	GUILayout.Label("Score: " + Score);
}

In lazer script:

void OnTriggerEnter(Collider collider) {
	if (collider.gameObject.tag == "Enemy")
		Player.Score++;
	}
}

More about static values on Unity Tutorials. Brace yourself, this is Intermediate level. :slight_smile:

I wouldn’t call static anything near Intermediate. It’s just a variable that lives throughout the application.
Also, your example only makes it add the score when the bullet hits the enemy, hopefully the person asking the question isn’t a total begginer, because of a few things.

1: You demonstrated this using OnTriggerEnter without disposing of the GameObject. Get ready for the score to jump 500 times every bullet.
2: He might not even know what a “Trigger” is.

Not saying your example wasn’t in the correct direction, but it could lead to a whole new problem.

His enemies could have health and don’t die till they hit 0, etc. We need more information.

It’s a copy from first post.
And about a level - you haven’t clicked on link, don’t you?

What about a level?