Collecting 10 items and win?

Hi. I’m very new to Unity and I’ve a question about collecting items in my game…

I got 10 items in my game, when the player needs to click on them, or walk into them to trigger some events.
What I want next is to create a counter that counts how many items the player has clicked/walked into.
when the counter counts to 10 something will happen…(like fireworks)
I’m able to make it count for entering the trigger, but not for mouse clicks…

This is the script I got on the first person controller
var score = 0;
var scoreText = “Items viewed: 0”;
var f : Font;
var fontSize : int = 20;

 function OnTriggerEnter(other : Collider ) {
 if (other.tag == "counter") {
 score += 1;
 scoreText = "Items viewed: " + score;
 Debug.Log("Score is now " + score);

 }
 }


 function OnGUI () {
 GUI.skin.font = f;
 GUI.skin.label.fontSize = fontSize;
 GUI.Label (Rect (200, 10, 200, 400), scoreText.ToString());
 }

I have a tag called “counter” for all the 10 items.
How can I get the counter also counts the mouse clicks?
Thanks for your time.

Hej there. You can use the Physics.Raycast class to fire a ray from the position the player clicked right into the scene of the game. The Items you want to collect already have a collider in order to be “walkable” so this should work:

    function Update () {
        if (Input.GetButtonDown ("Fire1")) {
            // Construct a ray from the current mouse coordinates
            var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            if (Physics.Raycast (ray)) {
                // player hit something
                score += 1;
            }
        }
    }

(modified this code here)

Though this code is triggered if any collider is hit so be sure to just increase the score if the player hit an item!

Okay I solved it now… I forgot to add this back in function Update :

scoreText = "Items viewed: " + score;

But anyway, thanks for your help!!:slight_smile: