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 unity_gMYsoJX8qvaxjQ · Mar 14, 2019 at 02:38 PM · scripting problembuttontextscorescore system

Central score counter for 3 score generating buttons

Hey folks,

I'm working on a mobile project that requires score to be collected from 3 score generating buttons. All three buttons have different point value assigned to them. I have: Snow button for 25 points. DJ button for 50 points. SnS button for 100 points.

I need to collect the score on a central collector but I'm having an issue. When I press the same button multiple times the counter works fine but as soon as I hit one of the other buttons, the counter resets back to 0 before adding the points from that button on.

I've seen this problem solved when colliders and OnTriggerEnter are involved but I am quite new to this so I'm struggling to apply those tutorials to my buttons.

Heres my code that I adapted from a tutorial:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ButtonClickScriptSnow : MonoBehaviour { public Button button; public Text text;

 public int currentPoints =0;
 public int currentScore;

 public void ClickButton()
 {
     if (button != null)
     {
         currentScore = currentPoints + 25;
         if (text != null)
         {
             text.text = "" + currentScore.ToString();
         }
     }
 }

}

I change the point value and the name of the code for each button. Any help is appreciated, thanks.

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

Answer by unity_gMYsoJX8qvaxjQ · Mar 14, 2019 at 04:04 PM

Fixed. Combined all three button codes into one and made more ClickButton functions.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ButtonClickScript : MonoBehaviour { public Button DJbutton; public Button SnSbutton; public Button Snowbutton; public Text text;

 public int currentPoints = 0;
 public int snowPoints;
 public int snsPoints;
 public int djPoints;

 public void ClickButton1()
 {
     if (DJbutton != null)
     {
         djPoints = djPoints + 50;
         currentPoints = djPoints + snowPoints + snsPoints;
         if (text != null)
         {
             text.text = "" + currentPoints.ToString();
         }
     }
 }
 public void ClickButton2()
 {
     if (SnSbutton != null)
     {
         snsPoints = snsPoints + 100;
         currentPoints = djPoints + snowPoints + snsPoints;
         if (text != null)
         {
             text.text = "" + currentPoints.ToString();
         }
     }
 }
 public void ClickButton3()
 {
     if (Snowbutton != null)
     {
         snowPoints = snowPoints + 25;
         currentPoints = djPoints + snowPoints + snsPoints;
         if (text != null)
         {
             text.text = "" + currentPoints.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

192 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 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

I want my score to reset back to 0 but keep my highscore saved 3 Answers

How to create a score manager script involving awarding points from multiple objects? 2 Answers

create a button with name and text for every game object with tag 0 Answers

Can't increase my score after OnTriggerEnter occurs. 1 Answer

Simple score system breaking crates 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