- Home /
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.
Answer by Mexallon · May 12, 2013 at 03:33 PM
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!
Thanks for your fast reply. I added this into my script, but I don't know how to specify which item will be the one that increases the score... I tried this:
if (Physics.Raycast (ray) && gameObject.tag == "counter") {
score += 1;
}
But it didn't work... Could you give me some advice? :(
Hm that should actually work. Did you assign the tag "counter" to the items that should increase the score?
Yes I did... it still didn't work. Also I had 2 more questions.. Should I put this script on the first person controller or on his main camera? Another problem is that the score is not updating in the scene for the mouse clicks, it only updates inside inspector..
Alright.. I solved the problem. It's because I turned on "Is Trigger" for all the items. :/ However the score still wouldn't update in the scene. Any ideas why this is happening??
Okay I solved it now.. I forgot to add this back in function Update :
scoreText = "Items viewed: " + score;
But anyway, thanks for your help!!:)
Answer by lmy13 · May 12, 2013 at 05:24 PM
Okay I solved it now.. I forgot to add this back in function Update :
scoreText = "Items viewed: " + score;
But anyway, thanks for your help!!:)
Your answer
![](https://koobas.hobune.stream/wayback/20220613102056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resouce script help 3 Answers
OnGUI not being called with NetworkBehaviour 0 Answers
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer
GUI Help, timer and counter 1 Answer
How to give a bonus every 1000 points? 2 Answers