How can i get a scoring system?
Hi.
I'm making my first game and its almost done. it just needs a scoring system. I have spent hours trying to find a tutorial but i just can't get it to work.
My goal is to make it so every time i kill an enemy (using prefabs) i would score a point and it would add 1 to the counter that is on the screen.
Thank you in advance.
ps. I'm using C#
Answer by Positive7 · Sep 08, 2015 at 10:25 PM
You didn't provide much information about your game or some code so here's a dirty example :
Test Scene setup Attach
ScoreManager.cs to a new GameObject
Attach EnemyScript.cs to a cube
Press Play Click 10 times with left mouse
Enemy gameObject should be Destroyed and Score should be set to 1
ScoreManager.cs
using UnityEngine;
public class ScoreManager : MonoBehaviour {
public static int Score;
public int _score {
get
{
return Score;
}
set
{
Score = value;
}
}
void OnGUI(){
GUILayout.Label ("Score : " + _score.ToString());
}
}
EnemyScript.cs
using UnityEngine;
public class EnemyScript : MonoBehaviour {
static int Health = 10;
void Update () {
if (Input.GetMouseButtonDown (0)) {
Health -= 1;
}
if(Health <= 0){
ScoreManager.Score += 1;
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
I can't figure out how to do this 2 Answers
Moving left and right with one button in 2d Game 1 Answer
Camera Movement Question - How to follow player in Y axis only above certain threshold? 0 Answers
How to check if player is rapidly rotating the joystick? 2 Answers
Object jumping between the touch 0 Answers