ok they are loaded
Great. So, our class can now access the Text GameObjects to output its calculated values to, and now that we have those references as member variables we can access them anywhere within our class. We also have a method for each characteristic and a more generic method which is responsible for effectively rolling the dice.
Lets put it together;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RollStats : MonoBehaviour {
public Text strength;
public Text dexterity;
public Text constitution;
public Text intelligence;
public Text wisdom;
public Text charisma;
private int DiceRoller()
{
// TODO: remove the hard-coded "4"
int[] results = Dice.Roll(Dice.DieType.D6, 4, Dice.SortOrder.Descending);
int sumOfDiceRolls = 0;
// TODO: remove the hard-coded "3"
for (int i = 0; i < 3; i++)
{
sumOfDiceRolls += results[i];
}
return sumOfDiceRolls;
}
public void RollStrength()
{
strength.text = DiceRoller();
}
public void RollDexterity()
{
// intentionally left empty
}
public void RollConsititution()
{
// intentionally left empty
}
public void RollIntelligence()
{
// intentionally left empty
}
public void RollWisdom()
{
// intentionally left empty
}
public void RollCharisma()
{
// intentionally left empty
}
}
I have updated the RollStrength
method, you can see that it now sets the strength GameObject’s text property to the returned value from our DiceRoller
method.
Update the script, run the game, text - you should see a value appear within the UI - Text GameObject for the strength characteristic.
Note: You may want to set the default text value (in the Inspector) for these UI-Text GameObjects to “0”, that way, they will appear as “0” before the button is clicked.
Assuming all is good and no errors occur, you can then update the other five characteristic methods in the same way.