x


score system for destroying objects

how can i make a point system, so that when a Gameobject of a particular Tag is destroyed, the points go up??

more ▼

asked May 08 '10 at 10:03 AM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

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

3 answers: sort voted first

Keeping score is simply a case of incrementing the value of a variable when the player does something that will earn them points. To show the score on screen you can use either a guiText or something like a guilabel:

// using a gui label
var currentScore : int = 0;

function OnGUI () {
    GUI.Label (Rect (10, 10, 100, 20), currentScore);
}

In the case of the guiText you would need to convert the value of the score (which is an integer) to a string, like so:

// convert the variable scoreNumber which is an integer to a string
    currentScore.ToString();

// set this to the text value of your guiText to display it
guiText.text = currentScore;

To increment the value of the currentScore variable, you could use something like the following. This is a modified extract from the Unity Game Development Essentials book where the player collects batteries by walking over them:

static var currentScore : int = 0;

    // A function specifically for detecting collisions with colliders
    // that have had the Is Trigger flag set in the inspector
    function OnTriggerEnter(collisionInfo : Collider){

        // if the current gameObject of the collider stored in the collisionInfo
        // variable has a tag of "battery" (ie, we have hit a battery)
        if(collisionInfo.gameObject.tag == "battery"){

            // Increment the global score variable declared in the by 1
            // ++ is short hand for currentScore = currentScore + 1;

            currentScore++;

            // Play a sound to confirm we have picked up a battery
            audio.PlayOneShot(batteryCollectSound);

            // Remove this particular battery from the scene (the object whose collider we have hit)
            Destroy(collisionInfo.gameObject);
        }
    }
more ▼

answered May 08 '10 at 11:23 AM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

Thank you very much! I keep forgetting to use .ToString()! But you reminded me and resolved my issue without me having to ask a question on here! Thanks a ton.

Jun 18 '10 at 01:31 AM xToxicInferno
(comments are locked)
10|3000 characters needed characters left

The best way is to notify another GameObject that keeps track of the score i.e. the player or a control GO. Something like this:

function ApplyDamage(damage){
  hp-=damage;
  if(hp<0){
    Control.SendMessage("AddPoints",200);
    Destroy(gameObject);
  }
}
more ▼

answered May 08 '10 at 11:01 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Wow, so many answers for such a simple question

May 08 '10 at 04:44 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

This page explains how to do scoring well. It is about the 4th item down.

To determined if points should be added, add points from the death of the enemy instead of the player. So the player attacks the enemy. The enemy calculates the damage and determines it should die. It calls the Die method.

function Die () {
    MySingleton.Instance.Score += 5;
    Destroy(gameObject);
}
more ▼

answered May 08 '10 at 11:04 AM

Peter G gravatar image

Peter G
15k 16 44 136

(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:

x2087
x764
x274
x49

asked: May 08 '10 at 10:03 AM

Seen: 5041 times

Last Updated: May 08 '10 at 10:03 AM