I am trying to display two scores and use them to trigger events.
I have two scores and I have set them to reset to 0 when the game starts. I had to use two scripts to do this, because it worked and I am sure there is a way to do it with one, but I am not sure how to do that.
code: public float scoreADisplay = 0;
public Text scoreA;
// Use this for initialization
void Start () {
scoreA=GetComponent<Text>();
scoreA.text = scoreADisplay.ToString("f0");
}
I have this for score A and Score B
But what I need to do is randomly choose between teamA or teamB, set that as the teamScored and then add 1 point to that team that scored.
I was looking into arrays, which seems like the right place to start, but I wasn't able to pull in the variables from a different script.
I think i need to set: teamA = false; teamB = false;
then do something like
rand between teamA or teamB set that to true and if true add 1 point to teamAScore or teamBScore
Any help would be awesome. Thanks.
Answer by cjdev · Jan 13, 2016 at 02:03 PM
You're just missing a couple tools: Random.value gives you a float between 0-1, GameObject.Find() will get a reference to another GameObject, and a couple methods can hold the logic.
public float scoreADisplay = 0;
public float scoreBDisplay = 0;
public Text scoreA;
public Text scoreB;
void Start () {
scoreA = GetComponent<Text>();
// Use the name of the GameObject scoreB is attached to
scoreB = GameObject.Find("NameOfGameObjectB").GetComponent<Text>();
UpdateDisplay();
//Use Score wherever you need to elsewhere
Score();
}
public void Score()
{
if (Random.value < 0.5f)
scoreADisplay += 1;
else
scoreBDisplay += 1;
UpdateDisplay();
}
public void UpdateDisplay()
{
scoreA.text = scoreADisplay.ToString("f0");
scoreB.text = scoreBDisplay.ToString("f0");
}
Your answer
Follow this Question
Related Questions
I'm trying to shuffle an array's order 3 Answers
null array? 0 Answers
Simple 2 Array bool Comprasion 1 Answer