- Home /
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.
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();
}
}
}
}
Your answer
Follow this Question
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