Scores/points when you destroy something?

Hi, im trying to make a FPS game which you battle some AI controlled NPCs. I need a script that can help accumulate points when i kill the NPC. Any help? thanks!

more ▼

asked Aug 21, 2010 at 10:30 AM

avatar image

user-3061 (yahoo)
126 82 57 65

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

1 answer: sort voted first

The abstract principle of this is very simple; it's mostly down to how you want to implement it. Basically you need to store a variable, Score, and each time an enemy is killed, increment that by one.

You could add the variable to the player object, or have an EmptyGameObject which holds stats and such, but the method is mostly the same. Say we add it to our player, called PlayerOne, which has a script called FirstPersonPlayer. In that script, add:

var playerScore = 0;

Then in the enemy NPC's death function (for example), add something like:

var thePlayer : GameObject = GameObject.Find("PlayerOne");
// creates a temp variable, then tries to find the player to assign it to that
// (this is messy, there are better ways, but it's easy to get started)
var playerScript: FirstPersonPlayer = thePlayer.GetComponent("FirstPersonPlayer");
// this variable refers to the script attached to the player we found
thePlayer.playerscore += 1;
// within that script, +1 to the score variable

That should hopefully be a good starting point, but you could make it better. For example, instead of playerScore += 1, you could have a variable in the enemy called "points" containing the score value of that NPC, and then change that last line to playerScore += points.

more ▼

answered Aug 21, 2010 at 11:59 AM

avatar image

Novodantis 1
1.8k 28 34 56

(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



Answers and Comments



asked: Aug 21, 2010 at 10:30 AM

Seen: 2920 times

Last Updated: Aug 21, 2010 at 10:30 AM