- Home /
How to create a review of previous updates
Hi everyone. Very simply put, I have a button which ends the player's turn and ages the character. I want the textbox to print of the player's age at the end of each turn using the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Eventmanager : MonoBehaviour
{
TextMeshProUGUI count;
// Start is called before the first frame update
void Start()
{
count = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
count.text = "Age: " + Character.Age + "\n";
}
}
Obviously, the above code just creates an area called "Age:" and updates the character's age on update. I want the script to create a new line and show the new age every time the age button is clicked. I'm assuming I will have to create a list that stores all previous ages however I'm yet to understand how I can do that. I have experimented with using a list but I keep getting "cannot implicitly convert" errors and several other problems.
TIA
Answer by IINovaII · Jan 20, 2020 at 03:15 AM
Your assumption is quite correct. A list will do the trick. The "cannot implicitly convert" error was probably due to converting an int to string. Since I don't know how age is implemented, the answer might not that accurate but hopefully give you an idea on how to solve your problem.
public class Eventmanager : MonoBehaviour
{
[SerializeField]
private Button _btnAge; // The button to increase age
[SerializeField]
private TextMeshProUGUI _txtAge;
private List<int> _storedAges = new List<int>(); //List with all the ages
private int _previousAge = 0; //The starting value of this field is whichever age your character starts with.
private void Awake()
{
_btnAge.onClick.AddListener(new UnityAction(() => BtnAge_OnClick())); // This will add the function BtnAge_OnClick as a listener which means whenever the button is clicked, this function will be executed
}
private void BtnAge_OnClick()
{
_previousAge++;
_storedAges.Add(_previousAge);
string ageString = "";
for (int i = 0; i < _storedAges.Count; i++)
{
ageString += "Age: " + _storedAges[i] + "\n";
}
_txtAge.text = ageString;
}
}
Thanks for your response. $$anonymous$$y aging mechanic is just a very simple script attached to the "Age" button as seen below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AgeScript : $$anonymous$$onoBehaviour
{
public static int PlayerAge;
public void AgePlayer()
{
PlayerAge++;
}
}
The current player age is then stored in the character script under Age as referenced in the code from OP. I had a feeling that this is a very roundabout way of doing this and I now think that is even more true. Should I transfer everything from the AgePlayer script into the script for the event manager? Not entirely certain how but should be able to figure it out.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221819im_/https://answers.unity.com/themes/thub/images/avi.jpg)