Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by HolBol · May 08, 2010 at 10:03 AM · gameobjectdestroyscorepoints

score system for destroying objects

how can i make a point system, so that when a Gameobject of a particular Tag is destroyed, the points go up??

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
5
Best Answer

Answer by straydogstrut · May 08, 2010 at 11:23 AM

Keeping score is simply a case of incrementing the value of a variable when the player does something that will earn them points. To show the score on screen you can use either a guiText or something like a guilabel:

// using a gui label var currentScore : int = 0;

function OnGUI () { GUI.Label (Rect (10, 10, 100, 20), currentScore); }

In the case of the guiText you would need to convert the value of the score (which is an integer) to a string, like so:

// convert the variable scoreNumber which is an integer to a string currentScore.ToString();

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

To increment the value of the currentScore variable, you could use something like the following. This is a modified extract from the Unity Game Development Essentials book where the player collects batteries by walking over them:

static var currentScore : int = 0;

 // 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 == "battery"){

         // 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
         audio.PlayOneShot(batteryCollectSound);

         // Remove this particular battery from the scene (the object whose collider we have hit)
         Destroy(collisionInfo.gameObject);
     }
 }

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 xToxicInferno · Jun 18, 2010 at 01:31 AM 0
Share

Thank you very much! I keep forgetting to use .ToString()! But you re$$anonymous$$ded me and resolved my issue without me having to ask a question on here! Thanks a ton.

avatar image
1

Answer by spinaljack · May 08, 2010 at 11:01 AM

The best way is to notify another GameObject that keeps track of the score i.e. the player or a control GO. Something like this:

function ApplyDamage(damage){
  hp-=damage;
  if(hp<0){
    Control.SendMessage("AddPoints",200);
    Destroy(gameObject);
  }
}
Comment
Add comment · Show 3 · 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 spinaljack · May 08, 2010 at 04:44 PM 0
Share

Wow, so many answers for such a simple question

avatar image kingrazer · Jun 26, 2013 at 08:15 PM 0
Share

these script gives me erros it says hp uknown idntifier and controll unkown identifier

avatar image J-R-Wood · Feb 19, 2014 at 03:17 AM 0
Share

you probably know this but. It says hp uknown idntifier and controll unkown identifier because you need to Define them

avatar image
0

Answer by Peter G · May 08, 2010 at 11:04 AM

This page explains how to do scoring well. It is about the 4th item down.

To determined if points should be added, add points from the death of the enemy instead of the player. So the player attacks the enemy. The enemy calculates the damage and determines it should die. It calls the Die method.

function Die () {
    MySingleton.Instance.Score += 5;
    Destroy(gameObject);
}
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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

score script + gameobject apears for 15 secends 0 Answers

Is there another way of counting Scores (points) instead of Colliding (destroying) 1 Answer

Destroying gameobject 2 Answers

Scoring (i'm stuck) 3 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