• 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 ILoveMyDada · Jun 23, 2017 at 05:25 AM · triggerupdatefunctionstringcount

How to Update a score count going up and down

Hi I'm trying to make it so that when a ball hits the ground, the score count goes down by 1.

I already have it where if the ball hits a moving box, then the score count goes up by 1, which is fine.

But with my code right now, if my score count is at lets say, 30. And if the ball hits the ground, I'd want it to go down to 29. But right now if the ball hits the ground, it starts at 0 and goes down to -1. And when the ball hits the box it goes back to the previous score of 30 and bumps it up to 31.

Any help would be appreciated, thanks!

 public GameObject ball;
 public Text countText;

 public float volume;
 public AudioClip SoundToPlay;
 AudioSource audio;

 private int count;

 void Start()
 {
     count = 0;
     SetCountText ();
     audio = GetComponent<AudioSource> ();

 }

 void OnTriggerEnter2D (Collider2D paddleTrigger)
 {
     if (paddleTrigger.CompareTag ("ball")) 
     {
         count = count + 1;
         SetCountText ();
         audio.PlayOneShot (SoundToPlay, volume);
     }

 }

 void SetCountText()
 {
     countText.text = "" + count.ToString ();
 }

//This script ABOVE ^^^^^^^^^^ is for the count to go UP when hitting the box (paddle)

//This script BELOW vvvvvvvvvv is for the count to go DOWN when hitting the ground

 public GameObject ball;

 public Text countText;

 public float volume;
 public AudioClip SoundToPlay;
 AudioSource audio;

 private int count;

 void Start()
 {
     
     audio = GetComponent<AudioSource> ();

     this.GetComponent<BoxCollider2D> ().enabled = false;

 }

 void UpdateCount()
 {
     SetCountText ();
 }



 void OnTriggerEnter2D (Collider2D groundTrigger)
 {
     if (groundTrigger.CompareTag ("ball")) 
     {
         count = count - 1;
         SetCountText ();
         audio.PlayOneShot (SoundToPlay, volume);
     }

 }
     

 void SetCountText()
 {
     countText.text = count.ToString ("");
 }

}

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

1 Reply

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

Answer by cstooch · Jun 23, 2017 at 05:54 AM

First off, these are two different scripts, right? (Yes, they would be, but I should ask in case I'm having a brain fart here.. lol)

What I'm seeing here that's a problem..

It looks like you have two separate count variables, and that's why they are giving you weird results when they hit the different colliders. You need to declare this variable only once, and somewhere where both scripts would have access to this count, for example a "Game Manager" singleton, or something like that, or even in a script that is on the object that is parent to these other two scripts. Then within this central area where both scripts are able to access the variable, you'd declare it like so: public static int count = 0; There's a lot of info out there on how to access variables in other scripts.

Just as a tip.. us coders can be lazy at times. count = count - 1 and count = count + 1 are too verbose for us lazy folks.

You can actually replace these with:

 count++;
 count--;

......

 count = count - 1;
 // the above and below do the same thing
 count--;

Next, here's what you'd want to do to prevent decrementing below 0:

 if (i > 0) i--;
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 ILoveMyDada · Jun 23, 2017 at 08:00 AM 0
Share

Yeah they are both separate scripts, haha.

You're response totally makes sense and I got it to work! Basically just made that top script declare the variables and functions and then called them in the 2nd script.

Thanks a lot!

avatar image cstooch ILoveMyDada · Jun 23, 2017 at 01:27 PM 0
Share

Yeah, that definitely would work. No problem!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

80 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

Related Questions

Update() doesn't update so well 1 Answer

How to count the number of strings in a string class? 2 Answers

Update function in a UnityScript attached to 50 clones of a prefab: heavy or not? 1 Answer

I can't trigger OnTirggerExit2D(Collider2D)!! 2 Answers

How can I use a yeild during an udate? 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