• 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 pariveshp · Apr 25, 2012 at 05:05 AM · score

score conter

i wrote a script for counting the score and saved as score but i am not sure where i have to place and how to display that score. I whole programe I have three object. 1. Player, 2. Enemy, 3. Bullet. Please help me. Please below the code for counting and tell its correct or not.

static var score:int=0;

function OnTriggerEnter(otherObject:Collider) { if(otherObject.gameObject.tag==("enemy")) { gameObject.Destroy(otherObject.gameObject); score ++;

 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by venkspower · Apr 25, 2012 at 05:11 AM

Use GUIText to display the score in your game view.

Comment
Add comment · 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
0

Answer by fafase · Apr 25, 2012 at 05:13 AM

the static var score goes in the player script, I will call it Player.js. The score belongs to him.

The second part I would attach it to the enemy and modify a little. I know it is logical but it makes thing easier to me

     var health = 100; // healt of your enemy
     function OnTriggerEnter(otherObject:Collider) { 
         if(otherObject.gameObject.tag==("bullet")) { 
              Player.score +=1;   // decrease score
              Destroy(other.gameObject);   // destroy otherObject, the bullet
              health -= 10;          // decrease health
              if (health<=0)        // check health
                    Destroy(gameObject);   // kill enemy
         }
    }

If you wanted to attach it to the bullet, it is possible BUT you would have to GetComponent() the script of the enemy to access the health. Do not make the health of the enemy static!!! Look on the forum about static.

Add a GuiText object to your scene GameObject->CreateOther->GuiText

Place it and attach a new script to it with

 function Update(){
    guiText.text = "Score: "+Player.score;
 }

You can also use OnGui but I find GuiText more flexible and easier to use.

Comment
Add comment · 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
0

Answer by sriram90 · Apr 25, 2012 at 05:14 AM

hi pariveshp,

its very simple. you can display score using OnGUI method. you have to use this method for display anything on User Interface.

 function OnGUI()
 {
  GUI.Label(Rect(Screen.width/4,Screen.height/16,Screen.width/2,Screen.height/2),"Score : "+score);
 }
Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Hi i been trying to add a local highscore to display when gameover but dont no how new to unity need help? 1 Answer

I'm trying to set a high score but I can't display it in another scene? 2 Answers

¿Score system with the new UI? 0 Answers

PlayerPrefs is acting weird ?? 1 Answer

Adding coin score to multiplied value 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