- Home /
Why does my text update at the beggining of the game but then it stops updating?
Hi
I create a Text on UI which is supposed to play as a life counter by decreasing a number each time the player dies and the scene reloads. The method "Failure" (please see below) is actually called upon this via another script when the player hits an obstacle or falls down from a height and it works perfectly. I'm trying to connect the LifeCounter Text on UI to my LifeManager which I made using "create empty". The Text displays exactly the number that I set for life in the LifeManager as the game starts, but the problem is that when the player dies and the method "Failure" is called, the Text which I named "LifeCounter" doesn't update in the game view (while in the inspector the life keeps reducing by each death). Btw, I made a reference to my Text in my LifeManager script by using the drag and drop technique but once the player dies the reference turns to "Missing". I'd appreciate it if someone please tell me what I'm probably missing and why my text (that shows the number of lives) in the game view doesn't update while in the inspector it does happen. Here are my codes:
private static LifeManager instance;
public int Life;
public Text LifeCounter;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(instance);
}
LifeCounter.text = Life.ToString();
}
public void Failure ()
{
Life = Life - 1;
}
@Siavash1989 You are putting LifeCounter.text = Life.ToString();
in void Awake Shouldn't it be in void Update()? quote from unity : "Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. "
Or put it in your Failure function after reducing the life, should work just fine.
public void Failure ()
{
Life = Life - 1;
LifeCounter.text = Life.ToString();
}
Answer by GrayLightGames · Oct 06, 2019 at 08:41 PM
I believe you just need to update the LifeCount.text inside the Failure method.
public void Failure ()
{
Life = Life - 1;
LifeCount.text = Life.ToString();
}
You may want to make another function called UpdateLifeText() and call that in case you later want to change how the text is updated or how you calculate lives remaing and then you won't have to change code in two places.
Hope that does the trick!
I tried
public void Failure () { Life = Life - 1; LifeCounter.text = Life.ToString(); }
It only updates the Text LifeCounter once. For example Life: 5 updates to Life: 4 when I hit an obstacle and the function Failure is called, but as soon as the scene reloads the Text LifeCounter becomes equal to 5 again and it never updates anymore, no matter how many times the function Failure is called next.
Hmmm... so if I'm following, you're reloading the scene when the player dies? Each time you reload a scene, all the GameObjects are destroyed and refreshed with the original instance values. If you don't want this to happen, you can prevent the GameObject from being destroyed by calling the following from Awake: DontDestroyOnLoad(this.gameObject); Then you'd need to update the text object when the scene reloads somehow... maybe by having another controller call your Life$$anonymous$$anager.UpdateLifeCounter() in its Awake.
You could also store the Life variable in PlayerPrefs or a file and then access it on Awake.
Or you could reload the level without reloading the scene. I haven't used the scene functionality much myself, it seems like it's more trouble than it's worth. If that's really counter to your game architecture, you could always make one specific GameObject with DontDestroyOnLoad that holds all the values you need to persist between scene loads. Then pull the values back in Awake or another function.
I'm not sure why the next time Failure is being called it wouldn't update to 4. $$anonymous$$ight be something else going on. DontDestroyOnLoad should sort it out though.
Answer by Siavash1989 · Oct 06, 2019 at 09:03 PM
I tried
public void Failure () { Life = Life - 1; LifeCount.text = Life.ToString(); }
It only updates the Text LifeCounter once. For example Life: 5 updates to Life: 4 when I hit an obstacle and the function Failure is called, but as soon as the scene reloads the Text LifeCounter becomes equal to 5 again and it never updates anymore, no matter how many times the function Failure is called next.
When your game reloads your variables are getting wipped out and made again i think, you need to either stop the reload, and only reduce the player lives. Or you need to get your vars from one sesion to the other:
Answer by Anaxis_Studio · Oct 06, 2019 at 09:48 PM
My first guess is it appears you are implementing the singleton pattern incorrectly.
public static LifeManager lifeManager;
void Awake()
{
if (lifeManager == null)
{
DontDestroyOnLoad(gameObject);
lifeManager = this;
}
else if (lifeManager != this)
{
Destroy(gameObject);
}
}
A couple of days ago, finally, I could find the solution after testing many ways. If anyone needs to know, let me know so I can help him/her avoid my mistakes while coding and working with Unity.
Your answer
Follow this Question
Related Questions
How to increase the life of the player? 2 Answers
Life System need help please!!! 1 Answer
Referencing all objects in scene 2 Answers
trouble with referencing a componenet (bool) 1 Answer
Instantiated Objects dont get their Script referenced properly 2 Answers