need help making a score board

Hi! I’m getting into unity and need help making a score board and have searched around for about a day before posting, but couldn’t find anything to help me if you could give some advice/examples that would be greatly appreciated. the script for my enemy health is here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float health = 1f; // the enemy health will stay 1
public float left = 1f; // sets speed of movement
void Start()
{
StartCoroutine(tofar()); // starts the timer to destroy the game object after a certain ammount of time
}
void Update()
{
transform.Translate(left, 0, 0); // moves the enemy to the left of the screen
if (health == 0)
{
Destroy(gameObject); // kills th enemy
}
}
IEnumerator tofar()
{
yield return new WaitForSecondsRealtime(8); // waits 8 seconds, will change based on how far it needs to travel
Destroy(gameObject);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == “knive”) // tracks collision with weapon could help track kills?
{
health = health - 1; // sets health to 0
}
}
}

Hey, welcome to Unity ! :slight_smile:

Few things I’d like to say about your code.

  • First of all, you totally should rename your script to something more understandable. Suggestion: Enemy
  • I would move the health = health -1 into it’s own method that would take care of the TakeDamage logic.This will let you remove the health check in the update method, you don’t need to check if the enemy health is 0 every frame, only when it takes damage !
private void TakeDamage(float damage)
{
   health -= damage;
   if(health <= 0)
   {
     //add score
     Destroy(gameObject);
   }
}
  • For the actual question (scoreboard), I would search for Singleton in Unity. I would implement a Singleton class that would keep track of the score for you. A singleton is accessible from anywhere in your game, so your enemy script could call an AddScore method onto that Singleton. You could also save the score via PlayerPrefs if you want to keep track of highscore between play sessions.

Have fun ! :slight_smile:

I couldn’t find the singleton thing in unity, and I’m sorry but I forgot to mention that I’m using unity 2d sorry if it does make a difference.

edit: I forgot to mention I found to a way to count the value as an integer but currently have no way to display this on screen, sorry if I’m being a bother.

It makes no differences, no worries ! Youtube is a great source for Unity lessons. Here’s a video covering the topic of singletons in Unity !

Video

I’m sorry rob but I didn’t understand this, it could be that I’m tired but I don’t think it’s what I’m looking for, I have made a system to count the killed enemies but have no knowledge on how to set it into the text that should display the kills, I’m sorry for not explaining it well at the beginning.

Hi, so to make a score screen, you do these things:

  1. Create a Canvas. Right-click Hierarchy, and select UI → Canvas
  2. Create a Text object and place it as a child of the Canvas. Right-click the canvas object in Hierarchy, and select UI → Text. (The canvas is required, so if you create a Text object without a Canvas, it will automatically create a Canvas for you, like in the youtube video below)
  3. In your script where you are keeping track of score, make a reference to the Text object, like “public Text scoreText;” (make sure you include the ‘using UnityEngine.UI;’ namespace at top)
  4. In the hierarchy, drag the Text object you created into the “scoreText” slot of the script. (Make sure your script is already attached to an object in the hierarchy)
  5. Now you can update the score inside your script by referring to the reference you created and linked, “scoreText.text = myIntScore.ToString()”

Here’s a YouTube video that kind of goes through all those steps (not necessarily exact code or naming conventions)

Good luck!

1 Like

thank you for the help, it works now!

1 Like