Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lmy13 · May 12, 2013 at 02:47 PM · onguicountercollect

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lmy13 · May 12, 2013 at 04:21 PM 0
Share

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

avatar image Mexallon · May 12, 2013 at 04:31 PM 0
Share

Hm that should actually work. Did you assign the tag "counter" to the items that should increase the score?

avatar image lmy13 · May 12, 2013 at 05:02 PM 0
Share

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..

avatar image lmy13 · May 12, 2013 at 05:13 PM 0
Share

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??

avatar image lmy13 · May 12, 2013 at 05:23 PM 0
Share

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

 scoreText = "Items viewed: " + score;

But anyway, thanks for your help!!:)

avatar image
0

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!!:)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mexallon · May 12, 2013 at 06:13 PM 0
Share

you're welcome

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges