Accessing text component.

Hi, I’m a beginning programmer and I am currently stuck on a problem: I want to make a counter that shows your total attempts while playing a level, I already implemented a way to add a number to the counter but I have troubles with accessing the text component. Can someone please help?

public Text attemptText;

public int attempts = 1;

attemptText.GetComponent().text = "Attempts: " + attempts;

Is the text component on the game object with this script? If not, it won’t work. Also, you need to pass strings only to the text.text, it cannot be a string and an int as you have it now. You can change your int to a string like so attempts.ToString(). Also, if that GetComponent<Text>() call is not inside a function, your code won’t even compile

You don’t need to call GetComponent<>() if you already have the component.

You already have the component Text, you named it attemptText and set it to public so that you could assign it in the inspector.

To change the text of a text component, just do

attemptText.text = "Attempt: " + attempts;