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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by harrietradcliffe · Jul 13, 2013 at 06:25 PM · 2d gamescore

Add score when object destroys (For Hidden Object Game)

Hi! So we've made a code in unity that could destroy an object and at the same time... add score:

 var Score3Dtext: TextMesh;
 
 private var score: int=1000;
 
         
 
 function Update () {
 
     if(Input.GetMouseButtonDown(0))
 
     {
 
         var hit: RaycastHit;
 
         var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);
 
         
 
         if(Physics.Raycast(ray, hit))
 
         {
 
             if(hit.transform.gameObject == this.gameObject)
 
             {
 
                 Destroy(this.gameObject);
 
                 score += 1;
 
                 Score3Dtext.text= "Score: " + score.ToString();
 
             }
 
         }
 
     }
 
 
 
 }

so... We've dragged it in our object. Once we click the object, it destroys and add 1 to the score... it becomes 1001, but whenever we click another object, it stays the same... 1001 but it will be destroyed, our only problem is about the scoring. please help! thank you.

Comment
Add comment · Show 1
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 Dothackking · Jul 13, 2013 at 06:33 PM 0
Share

Did you put the script on the other objects?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Kwoth · Jul 13, 2013 at 08:51 PM

You cannot keep the track of a score on the objects that are going to be destroyed, whenever new object spawns, your score is 1000 again, but you see 1001 because you are not updating it until you destroy that object, so when you do , it goes back to 1001. You will have to make a separate script that will hold you score, and access it from this script. The way i would do it is make a tag called "ScoreBoard" and put it on your 3dText. make new script called "Scoring" and add it to that 3dtext:

 #pragma strict
 var Score3dText: TextMesh;
 var score=1000;
 function Update(){
     Score3dText.text="Score : " + score.ToString();
 }

It will keep your score. Now you need to access it from your object that will be destroyed. This is your script slightly modified:

 private var Score3Dtext: GameObject;
 function Start(){
 Score3Dtext= GameObject.FindGameObjectWithTag("ScoreBoard") ;
 
 }
  
 function Update () {
  
     if(Input.GetMouseButtonDown(0))
  
     {
  
        var hit: RaycastHit;
  
        var ray: Ray=Camera.main.ScreenPointToRay (Input.mousePosition);
  
  
  
        if(Physics.Raycast(ray, hit))
  
        {
  
          if(hit.transform.gameObject == this.gameObject)
  
          {
           Score3Dtext.GetComponent(Scoring).score+=1;
          Destroy(this.gameObject);
  
          }
  
        }
  
     }
 }

Dont forget to drag your 3dtext onto its Scoring script. Everything will work fine now.

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 Dothackking · Jul 13, 2013 at 08:55 PM 0
Share

Don't forget about the Send$$anonymous$$essage() function, i'm pretty sure it's easier on the processing than GetComponent

avatar image harrietradcliffe · Jul 14, 2013 at 04:11 PM 0
Share

ok! thanks a lot!!! we'll try that!

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

17 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 avatar image avatar image

Related Questions

Creating the score in hidden object game 1 Answer

Scoring using the edge collider 2D help! 2 Answers

Unity2D background image ui element covering player sprite and score(Text ui element) 1 Answer

Multiplying score on a gameobject destroy; only while a button is held 0 Answers

how to stop the score from adding upon death? 0 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