In general you can simply change that via
someTmpText.fontStyle = FontStyles.Italic;
Then as said I would rather insert some randomness instead of a hard-coded code.
First of all I would have an enum
for simply having names and values together:
public enum Numbers
{
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine
}
and then there are two different UI items
- Your number buttons
- The indicator texts for which buttons to press
I would have
- a dedicated component for each
- a prefab for each
The component attached to the button prefab
public class NumberItem : MonoBehaviour
{
[SerializeField] private Button button;
private Action<Numbers> clickCallback;
private Numbers value;
private void Awake()
{
button.onClick.AddListener(OnClick);
}
public void Initilaize(Numbers value, Action<Numbers> clickCallback)
{
this.value = value;
this.clickCallback = clickCallback;
}
private void OnClick()
{
clickCallback?.Invoke(value);
}
}
and for the UI display text e.g.
public class NumberIndicator : MonoBehaviour
{
[SerializeField] private TMP_Text text;
public void Initialize(Numbers number)
{
text.text = number.ToString();
}
public void SetPassed()
{
text.fontStyle = FontStyles.Strikethrough;
}
}
and then finally as mentioned do some randomization of the numbers and do
// get an array of all available numbers once
private static readonly Numbers[] _allNumbers = (Numbers[])Enum.GetValues(typeof(Numbers));
public SceneManager sceneManager;
public SceneNames targetSceneName;
// The two prefabs to be spawned
[SerializeField] private NumberItem _NumberButtonPrefab;
[SerializeField] private NumberIndicator _NumberIndicatorPrefab;
// The two parents that those prefabs will be spawned as child as
[SerializeField] private Transform _NumberButtonsContainer;
[SerializeField] private Transform _NumberIndicatorsContainer;
// configure the length of the code
[SerializeField] private int _codeLength = 5;
// will hold the current code sequence
private Numbers[] _currentSequence;
// index like you had already
private int _index;
private readonly List<NumberIndicator> _NumberIndicatorInstances = new List<NumberIndicator>();
private void Start()
{
InitSequence();
}
private void InitSequence()
{
// create a new random sequence without repeating from available numbers of given _codeLength
_currentSequence = _allNumbers.OrderBy(_ => Random.value).Take(_codeLength).ToArray();
// reset input index
_index = 0;
// Destroy all number indicators and buttons
// you could use pooling of course but this is just easier and impact should be neglectable
foreach(var indicator in _NumberIndicatorInstances)
{
Destroy(indicator.gameObject);
}
_NumberIndicatorInstances.Clear();
foreach (Transform child in _NumberButtonsContainer)
{
Destroy(child.gameObject);
}
// spawn indicators in order and set display name
foreach (var number in _currentSequence)
{
var numberIndicator = Instantiate(_NumberIndicatorPrefab, _NumberIndicatorsContainer);
numberIndicator.Initialize(number);
_NumberIndicatorInstances.Add(numberIndicator);
}
// spawn buttons in again shuffled order and set value and callback
foreach (var number in _currentSequence.OrderBy(n => Random.value))
{
var numberItem = Instantiate(_NumberButtonPrefab, _NumberButtonsContainer);
numberItem.Initilaize(number, HandleClick);
}
}
// handle the button clicks as before
private void HandleClick(Numbers value)
{
// unexpected input => reset and start new sequence
if (_currentSequence[_index] != value || _index >= _currentSequence.Length)
{
Debug.LogError("Wrong input!");
InitSequence();
return;
}
// otherwise mark indicator as passed and go to next
_NumberIndicatorInstances[index].SetPassed();
// prepare for next input
_index++;
// if reaching last index go to scene
if (_index == _currentSequence.Length)
{
sceneManager.LoadScene(targetSceneName);
}
}
}