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!
Answer by Novodantis 1
Aug 21, 2010 at 11:59 AM
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
How to create a point/score system based on player performance.
Points when an enemy dies.
How to run an animation clip on button down-hold?
Heya again guys how about rotation and score board
Can't get this code to work