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
0
Question by Nova-1504 · Dec 06, 2016 at 12:25 AM · scripting problemuiscorescore systempoints

Score counter breaking after adding points

I managed to make a score counter that adds 500 points when an object is destroyed. The destroy function only destroys the renderer and collider of the destructibles. However, upon activating the script to add 500 points when already at 500, instead of going to 1000, it goes to 0 and then to 500 again. The only way to get above 500 is to destroy several crates at once. Here is the script:

 using UnityEngine;
 
 using UnityEngine.UI;
 using System.Collections;
 
 
 
 public class ScoreCounter : MonoBehaviour {
 public Text countText;
 private int score;
 
     void Awake () {
         score = 0;
         SetCountText ();
     
     }
 
     void Update () {
         
     
     }
 
     void OnCollisionEnter (Collision coll) { 
     if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
     score = score + 500;
         SetCountText ();
         }
     }
     public void SetCountText ()
     {
     countText.text = "Score: " + score.ToString ();
     }
 }
 
 
 
 

Why?! Please help, need an answer by tomorrow!
-EDIT- Now it doesn't give me any points at all. I think this whole problem is because it's attached to 41 objects at once, but I don't know how to work around that. I barely was able to do this script as is.

Comment
Add comment · Show 4
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 Nova-1504 · Dec 06, 2016 at 01:25 AM 0
Share

Really need an answer FAST!

avatar image Nova-1504 Nova-1504 · Dec 06, 2016 at 01:48 AM 0
Share

Because if I don't get one I have nothing to present tomorrow

avatar image Commander-Rabbit · Dec 06, 2016 at 02:02 AM 0
Share

$$anonymous$$aybe ins$$anonymous$$d of making score = 500 + score make 500 += score

avatar image roman_sedition · Dec 06, 2016 at 08:00 AM 0
Share

The problem is because you have 41 objects in the screen which are all updating the same text field. You should only have 1 object in the scene which is keeping the score and is not interact-able with any of the objects.

2 Replies

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

Answer by Paricus · Dec 06, 2016 at 07:41 AM

Hi. If you get this in time, you need to create an empty game object by going to the create tab (in the hierarchy view) and selecting empty game object. then add the new score script to that empty game object, not the crates.

create a new script the name CrateScript, thats very important, and attach that script to your crates. now on the crate, you will see the script attached (in the inspector view) and a field saying Score Script, drag the empty game object (from the hierarchy view, not the scene) directly onto that field that says Score Script. Its probably i good idea to do that, then duplicate that crate a bunch of times, so you don't have to repeat the process of dragging the empty game object onto the Score Script field.

   using UnityEngine;
   using UnityEngine.UI;
   using System.Collections;
   
   public class ScoreCounter : MonoBehaviour {
   public Text countText;
   int score = 0;
   
       public void SetCountText (int crateValue)
       {
       score += crateValue
       countText.text = "Score: " + score.ToString ();
       }


  using UnityEngine;
   using UnityEngine.UI;
   using System.Collections;
   
   public class CrateScript : MonoBehaviour {

       public ScoreCounter scoreScript;
       int crateValue = 500;

       void OnCollisionEnter (Collision coll) { 
       if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
       scoreScript.SetCountText(crateValue);
       Destroy(col.gameObject, 2);
           }
       }

   }
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 Nova-1504 · Dec 06, 2016 at 02:47 PM 0
Share

It worked perfectly!!! Thank you!

avatar image Paricus Nova-1504 · Dec 06, 2016 at 02:53 PM 0
Share

np, glad it works. Hope your presentation went well.

avatar image
0

Answer by Commander-Rabbit · Dec 06, 2016 at 03:06 AM

maybe try

 using UnityEngine;
  
  using UnityEngine.UI;
  using System.Collections;
  
  public class ScoreCounter : MonoBehaviour {
  public Text countText;
  private int score;
  
      void Awake () {
          score = 0;
          SetCountText ();
      
      }
  
      void OnCollisionEnter (Collision coll) { 
      if (coll.relativeVelocity.magnitude > 25f && coll.other.tag == "Crate") {
      500 += score;
          SetCountText ();
          }
      }
      public void SetCountText ()
      {
      countText.text = "Score: " + score.ToString ();
      }
  }

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

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

Related Questions

Double Score Problem ? 1 Answer

How to make a score screen or something related 1 Answer

points system gets overrwriten 3 Answers

Central score counter for 3 score generating buttons 1 Answer

I want my score to reset back to 0 but keep my highscore saved 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