x


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
more ▼

asked May 12 '11 at 02:02 PM

simpleone gravatar image

simpleone
110 36 40 46

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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...

more ▼

answered May 12 '11 at 02:34 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

When I add them I just get alot of errors -

Insert a semicolon at the end.

Unexpected token: int.

May 12 '11 at 02:57 PM simpleone

Hm, are you adding this to a javascript file?

May 12 '11 at 04:48 PM Ejlersen

The above code is in C#

May 12 '11 at 04:53 PM GesterX

by looking at this code it looks like points will always be 0, you aren't increasing by anything...

May 12 '11 at 05:08 PM sp00ks

GesterX: Yes, thats why I'm asking if he is adding it to a JavaScript file :)

May 12 '11 at 05:42 PM Ejlersen
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 12 '11 at 08:17 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

Can edit your script above it? I added what you told me, nothing happened - I probably wrote wrong ...

May 12 '11 at 09:47 PM simpleone

Done. Remember to assign the GUIskin in the inspector.

May 12 '11 at 10:09 PM Ejlersen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3416
x280
x101
x64

asked: May 12 '11 at 02:02 PM

Seen: 2192 times

Last Updated: Jun 13 '11 at 05:40 AM