x


Point System and pickup

Hi I am wondering how you could create a simple point system using a GUI and picking up an object.

Thanks!

more ▼

asked Nov 23 '10 at 07:57 PM

chris 3 gravatar image

chris 3
17 3 4 4

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

2 answers: sort oldest

The simplest answer I can think of is:

  1. Make the score a public static variable so that it can be accessed from anywhere (not necessarily the best choice in terms of software design, but nevertheless probably the most straightforward solution).

  2. Give the items to be picked up colliders, and check the 'is trigger' checkbox for them in the inspector.

  3. Add a script to the player object or the item objects that includes an OnTriggerEnter() function. In this function, check if the other object has the right name/tag/etc. (if necessary), and if it does, increase the score as appropriate and destroy the item object.

  4. Create a script with an OnGUI() function, attach it to some game object or other, and display the score using GUI/GUILayout.Label().

more ▼

answered Nov 23 '10 at 08:29 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

Thank you! This is very helpful.

Nov 29 '10 at 04:23 PM chris 3
(comments are locked)
10|3000 characters needed characters left

Hi..

This is the main script i have for my score system or point system. Name this script Score.

Then,

-create a sphere.

-add Sphere Collider on sphere

-tag the Sphere as Coin

-add this javascript to your character and your ready to go.

You have a pick up system with score on your screen(GUI).

Thank you.

var score = 0;
var scoreText = "Score: 0";
var mySkin : GUISkin;

function OnTriggerEnter( other : Collider ) {
    Debug.Log("OnTriggerEnter() was called");
    if (other.tag == "Coin") {
        Debug.Log("Other object is a coin");
        score += 1;
        scoreText = "Score: " + score;
        Debug.Log("Score is now " + score);
        Destroy(other.gameObject);
    }
}

function OnGUI () {
    GUI.skin = mySkin;
    GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }
more ▼

answered Nov 23 '10 at 09:42 PM

tomeromero gravatar image

tomeromero
75 15 15 22

Thank you for your help!

Nov 29 '10 at 04:22 PM chris 3

hey im planning on using this, but i want it so that i get if my turret is destroyed could you please help, if you need it here is the script for my turret destruction: var explosion : Transform; function OnTriggerEnter(hit : Collider) //function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "wormProjectile") { Destroy(hit.gameObject); var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);

                Destroy(gameObject);
                }

}

Apr 19 '11 at 02:15 AM jacques 1

For this script, I have tried it but the counter doesn't change. How do I fix this? this may help you and me

Mar 29 '12 at 03:15 AM fjcym

Your above explanation is clear but the script does not work.

Oct 06 '12 at 03:51 PM munaeem
(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:

x205
x143
x132

asked: Nov 23 '10 at 07:57 PM

Seen: 3118 times

Last Updated: Oct 06 '12 at 03:51 PM