Hello, I’am trying to make quiz game, but I’m getting trouble. I get StackOverflow Exception in my code. and the unity editor get force close when i trying to run the game. I will include link to my code and show the pict warning from unity.
here the link and pict
- Picture Warning From unity editor console
- Link quizUI Script
link: quizUI.cs - Google Drive
- Link quizManager Script
link: quizManager.cs - Google Drive
you need backing fields for these getters:
public Text ScoreText
{
get
{
return ScoreText;
}
}
public Text TimerText
{
get => TimerText;
}
If you simply try to return the property name, which is what you are doing in the getter, you have created a recursive situation that never ends (e.g. TimerText has no way to get any value on its own - it is simply a wrapper for a value that you haven’t created in your code ) Hence the stack overflow error.
To put it another way, getters and setters are really functions. Essentially you have a function that does this:
// function are placed on the stack when they are called,
// in your example the code does the below,
// and ends up calling the function ad infinitum, and the system runs out of stack space.
public ScoreText ScoreText(){
return ScoreText();
}
//whereas what you really want (in function form) is:
public string scoreText;
public string ScoreText(){
return scoreText;
}
// that is the same thing as a getter that works properly
So, if you want to use getters as you are trying to do, declare a backing field and give them values for each like you did with the other getters.
thanks for the solution, and im sorry because i’am newbie in a game developer