- Home /
How to add string once to the UI text, but update the substring every frame in unity?
Hello, I'm new to unity. I'm trying to show active effects in my game which player gained, and then show the effect name and time when the effect will finish in UI Text. But I have some problems. I created code example where you can understand better what I want.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ActiveEffects : MonoBehaviour { public Text EffectsText; public GameObject ActiveEffectsPanel; public float olddaysnumber; public float sodarknumber; // Start is called before the first frame update void Start() { olddaysnumber = 1; sodarknumber = 1; }
// Update is called once per frame
void Update()
{
ShowActiveEffects();
}
void ShowActiveEffects()
{
ActiveEffectsPanel.SetActive(true);
if (olddaysnumber == 1)
{
EffectsText.text += "Nostalgia" + " " + (300f - OldDaysTime).ToString(); // I didn't assign time of the effect in this script to you to understand better what I want.
}
if (sodarknumber == 1)
{
EffectsText.text += "SoDark" + " " + (300f - SoDarkTime).ToString(); // I didn't assign time of the effect in this script to you to understand better what I want.
}
}
}`
I have 2 effects. This is not the full code, because it's enormous, it's just piece of full code. When I run game I see this: /https://res.cloudinary.com/df9lgxsyr/image/upload/v1603868428/Hell_obuzq3.png I know where is the problem but I don't know how to solve it. I need to update the text once but the time I need to update must be updated every frame. Something like this: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ActiveEffects : MonoBehaviour { public Text EffectsText; public GameObject ActiveEffectsPanel; public float olddaysnumber; public float sodarknumber; public float stop; public float stop2; // Start is called before the first frame update void Start() { olddaysnumber = 1; sodarknumber = 1; stop = 1; stop2 = 1; }
// Update is called once per frame
void Update()
{
ShowActiveEffects();
}
void ShowActiveEffects()
{
ActiveEffectsPanel.SetActive(true);
if (olddaysnumber == 1)
{
if (stop == 1)
{
EffectsText.text += "Nostalgia" + " " + (300f - OldDaysTime).ToString(); // I didn't assign time of the effect in this script because it's script you to understand what I want.
stop = 0;
}
}
if (sodarknumber == 1)
{
if (stop2 == 1)
{
EffectsText.text += "SoDark" + " " + (300f - SoDarkTime).ToString(); // I didn't assign time of the effect in this script because it's script you to understand what I want.
stop2 = 0;
}
}
}
}
But here the time updates only once but it must update every frame. And all effect's name and time must be written only in one UI Text. Does somebody know the answer? Thanks for help, Teymur, and sorry for my English.
Your answer
Follow this Question
Related Questions
Text Not updating 1 Answer
uGUI Text not updated until RectTransform has been altered 1 Answer
Text component changes the text but doesn't remove the previous version. 0 Answers
Multiple gui instantiation issues 0 Answers
Text UI not updating 2 Answers