- Home /
Kill Counter --> Score
Hey,I've been searching around a lot after a kill counter script for like 2 days now but I can't find anything that actually really works - I want my score to increase everytime I kill a zombie with like 5 points or something...
I know that I gotta have a script that tells my GUI text to change and another one to tell it when the zombie dies but I can't still get it to work... Anyone know an example I can look on or an answered question that works?
/ Thanks
Answer by Ejlersen · May 12, 2011 at 02:34 PM
You could add a script to your zombies, e.g.:
public class Zombie : MonoBehaviour { public int points = 1; // You can edit this in the inspector
void OnDestroy()
{
PointCounter.points += points;
}
}
The GUI script would then be:
public class PointCounter : MonoBehaviour { public GUISkin guiSkin = null; public static int points = 0;
void OnGUI()
{
GUI.skin = guiSkin;
GUI.Label(new Rect(0.0f, 0.0f, 128.0f, 32.0f), points.ToString());
GUI.skin = null;
}
}
So, whenever a zombie is destroyed it will add the points its worth to the pointer counter.
GUISKIN: Read the answer below...
When I add them I just get alot of errors -
Insert a semicolon at the end.
Unexpected token: int.
by looking at this code it looks like points will always be 0, you aren't increasing by anything...
GesterX: Yes, thats why I'm asking if he is adding it to a JavaScript file :)
Answer by Ejlersen · May 12, 2011 at 08:17 PM
Just going to create a new answer for the comment:
... but how can I add a font or a texture to it? ...
You can use a GUISkin for that. You create the GUISkin in Unity in Project Hierarchy, then in the GUISkin you can choose label. In there you can set a font, size, and so on.
You use the GUISkin in the code by adding: GUI.skin = mySkin;
in OnGUI()
just before the label. You can then add the GUISkin by creating a public variable in the top of PointCounter
with points. Then you can drag the GUISkin on to the GUISkin variable in the inspector.
Can edit your script above it? I added what you told me, nothing happened - I probably wrote wrong ...
Your answer
Follow this Question
Related Questions
How to make a Kill Counter 1 Answer
How to "Increase score with Y axis"? 1 Answer
Score display error 1 Answer
Trying to get score counter... 2 Answers
Gun Script not working help please 2 Answers