• 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 James 17 · Apr 21, 2011 at 04:26 PM · score

Scoring without FPS Or timer

Hello Im trying to make a small Unity game in which the player explores a large free roam world in search of "Coins", Where each coin that collides with the First Person Controller will add to the score in a GUIText. I have the window set up to show my score but i do not know how to link all the components so that the score will appear in the Window. I tried using this code. . .

var windowRect : Rect = Rect (20, 20, 120, 50); function OnGUI () { windowRect = GUI.Window (0, windowRect, WindowFunction, "Score"); } currentScore.ToString(); static var currentScore : int = 0; function WindowFunction (windowID : int) { // convert the variable scoreNumber which is an integer to a string

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

// 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 == "Coin"){
// 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 // Remove this particular battery from the scene (the object whose collider we have hit) Destroy(collisionInfo.gameObject); }
} }

And im guessing several of you could point out the problems in my script. I am relatively new to Unity and Java. I appreciate any help.

Comment
Add comment · Show 2
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 Statement · Apr 21, 2011 at 04:29 PM 0
Share

A friendly re$$anonymous$$der: Format your code before posting, it'll yield better answers as people take the effort to actually read your code that way :)

avatar image burgunfaust · Apr 21, 2011 at 04:37 PM 0
Share

I think Statement borked your code in formatting it. Try reposting it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Scribe · Apr 21, 2011 at 06:48 PM

var windowRect : Rect = Rect (20, 20, 120, 50); var currentScore : int = 0; var Tag = "Coin";

 function OnGUI () {
     windowRect = GUI.Window (0, windowRect, WindowFunction, "Score: " + currentScore);
 }

 function WindowFunction (windowID : int) {
     currentScore.ToString();
 }

 function OnTriggerEnter(collisionInfo : Collider){
 if(collisionInfo.gameObject.tag == Tag){
     print("collided");          
     currentScore ++;
     print(currentScore);         
     // Play a sound to confirm we have picked up a collectable
     Destroy(collisionInfo.gameObject); 
     }    
 }

hope this helps, I've just tidied up your code and reorganized it to make a bit more sense. with these changes I got it to work for myself so hopefully you can get it to work

Scribe

Comment
Add comment · Show 2 · 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 James 17 · Apr 25, 2011 at 03:25 PM 0
Share

Thank you for your aid in Re-Scripting my GUI. However i am still relatively new to Unity and scripting so i have a few remaining questions. 1. I attached the GUI to both the object named "Coin" and the first person controller with no immediate success. The score is displayed, however the object does not dissapear nor give any points to the overall score when touched. 2. Was there a part of the revised code i was supposed to change? I am not familiar with the if(collisionInfo.gameObject.tag == Tag){ line in the script. I appreciate the help youve already given me, and any additional help.

avatar image Scribe · Apr 25, 2011 at 04:54 PM 0
Share

In your script you are asking it to do the stuff if the collectable object is tagged "Coin" you must put this script on your character and change the tag of the collectable item to "Coin". On the coin make sure you have a collider which has the is Trigger box ticked. sorry for the lack of explanation

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

¿Score system with the new UI? 0 Answers

PlayerPrefs is acting weird ?? 1 Answer

Adding coin score to multiplied value 2 Answers

Show score on gameover screen? 1 Answer

Count scripting problem 1 Answer


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