- Home /
How do I get Text Mesh to appear in the scene ten seconds later and disappear
I need the text mesh to appear and disappear in scene right at ten seconds or less . I got most of the code done . I only got one error :
(7,12): error CS0118: oik.text' is a
field' but a `type' was expected
Here is my code :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class oik : MonoBehaviour {
private text text;
void ShowMessage(string message, float timeToShow = 10)
{
StartCoroutine(ShowMessageCoroutine(message, timeToShow));
}
IEnumerator ShowMessageCoroutine(string message, float timeToShow = 10)
{
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
text.text = " The Reqiure Score is 1700" ;
}
}
Answer by importguru88 · Sep 01, 2016 at 02:23 AM
I have done that nothing has change the text is their on screen . Here is my code now :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class oik : MonoBehaviour {
private Text text;
public float timeShown = 0f;
void ShowMessage(string message, float timeToShow = 10)
{
StartCoroutine(ShowMessageCoroutine(message, timeToShow));
}
IEnumerator ShowMessageCoroutine(string message, float timeToShow = 10)
{
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
text.text = " The Reqiure Score is 1500" ;
}
}
Sorry, if I understand you correctly, you want it to display the message for 10 seconds and then hide it? If so, then this should be the code you want:
IEnumerator Show$$anonymous$$essageCoroutine(string message, float timeToShow = 10)
{
// Show the text
text.text = message;
// Wait for 10 seconds
float timeShown = 0f;
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
// Hide the text
text.text = "";
}
I am not using text mesh. I am using ui text. Will this still work for ui text ?
Yes, definitely. I've just got home and tried it on my own computer, and the code I gave you is working as expected. If it's not working for you, then there must be something else causing the issue. Could you provide a screenshot of the Inspector or something to give us more detail?
Answer by instruct9r · Aug 31, 2016 at 07:11 PM
public class oik : MonoBehaviour {
private Text text;
The definition of Text variable should be with Capital letter...
I don't have that error any more . I now got another error : (20,16): error CS0103: The name `timeShown' does not exist in the current context
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class oik : $$anonymous$$onoBehaviour {
private Text text;
void Show$$anonymous$$essage(string message, float timeToShow = 10)
{
StartCoroutine(Show$$anonymous$$essageCoroutine(message, timeToShow));
}
IEnumerator Show$$anonymous$$essageCoroutine(string message, float timeToShow = 10)
{
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
text.text = " The Reqiure Score is 1700" ;
}
}
You've defined timeToShow, but I don't see anywhere you've set up timeShown. Is timeShown supposed to be a variable from another script?
What do mean variable from another script ? Do you $$anonymous$$d testing my script ?
Your answer
Follow this Question
Related Questions
How do I change my health bar to be text only 2 Answers
Text Mesh Pro: highlight all words in a link 0 Answers
Custom Font works in editor but not in build 2 Answers
How do i make Text Mesh Pro appear on game display? 1 Answer
Predict Text Width of String 0 Answers