- Home /
How to make text pop up for a few seconds?
Like in Call of Duty, when you get a kill, your XP pops up for a second and then disappears. How would this be done in Unity. If you could give me and example in a script, preferably C#, but JavaScript is fine too, that'd be great. Thanks in advanced.
Answer by Eric5h5 · Sep 06, 2013 at 11:43 PM
In some function:
if (kill) {
StartCoroutine(ShowMessage("Abc", 2));
}
Along with:
IEnumerator ShowMessage (string message, float delay) {
guiText.text = message;
guiText.enabled = true;
yield return new WaitForSeconds(delay);
guiText.enabled = false;
}
In my project, there are some errors with this code. please help me. Errors are:-
Cannot implicitly convert type 'UnityEngine.WaitForSeconds' to 'string'
The name 'kill' does not exist in the current context
'System.Collections.Generic.IEnumerable' to 'string'
The if (kill)
part is just showing that the StartCoroutine call is to be made when you want the kill to happen. If using the first code snippet verbatim, you'd need to have declared a boolean kill
variable (and used whatever logic you need, to set it to true when you want the kill).
The type conversion error suggests that you've set the return type of your Show$$anonymous$$essage function to string
ins$$anonymous$$d of IEnumerator
as shown in the second snippet, which you can't do (as it's a coroutine)
GUIText.text is obsolete and will be removed in future versions.
What would be the change made so this answer works?
This answer is already pretty generic, there's no mention of GUIText.text
or the type GUIText
.
It works if the variable guiText
is of any monobehaviour type that has a text
property/field. It's just showing how to do something, wait a few seconds then do something else.
So, for example, guiText
could be of type UnityEngine.UI.Text
.
The answer is from 2013; what do you think Unity had for GUI functionality back then?
Answer by dokva · Apr 13, 2016 at 03:54 PM
public void MouseIsIn(){
this.transform.localScale=new Vector3 (1.2f,1.2f,1f);
}
public void MouseIsOut(){
this.transform.localScale=new Vector3 (1f,1f,1f);
}
and use it with event trigger that sits on text @TheMagzuz
Answer by tw1st3d · Sep 06, 2013 at 10:48 PM
Here's a quick little script for what you're looking for.
using UnityEngine;
using System.Collections;
using System;
class ShowTextForAFewSeconds : MonoBehavior
{
/*
* Start Copying Here
*/
private bool showText = false, someRandomCondition = true;
private float currentTime = 0.0f, executedTime = 0.0f, timeToWait = 5.0f;
void OnMouseDown()
{
executedTime = Time.time;
}
void Update()
{
currentTime = Time.time;
if(someRandomCondition)
showText = true;
else
showText = false;
if(executedTime !== 0.0f)
{
if(currentTime - executedTime > timeToWait)
{
executedTime = 0.0f;
someRandomCondition = false;
}
}
}
void OnGUI()
{
if(showText)
GUI.Label(new Rect(0, 0, 100, 100), "Some Random Text");
}
/*
* Stop Copying
*/
}
Had some problems with my script copying over, so I reposted a new answer with everything intact.
I tried your code tw1st3d, and it says error.
error CS0037: Cannot convert null to `float' because it is a value type
@ line:
private float currentTime = null, executedTime = null, timeToWait = 5.0f;
Not sure how to fix that in C#
Oh wow, sorry about that. Float's can't be set to a null value, so changing them to equal 0.0f will solve the error.
To change the side just tweak the last two integers, (0, 0, 100, 100), "Some Random Text"); the two 100s and that should change the size. Hope this helps, if you are still confused just @ me and I might be able to help!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
GUI text blinking opacity 1 Answer
Read objects like in Penumbra 1 Answer
Multiple Cars not working 1 Answer
GUI Problem 2 Answers