- Home /
How to create a blinking text and disabling it when the user clicks on the screen.,what would be the code to display a blinking text "TAP TO PLAY" and make it disable when the user touches the screen.
I am almost done with my game but the last thing I need is a blinking text "TAP TO PLAY" which can be disabled when the user clicks on the screen.,I am almost done with my game but I need to add a TAP TO PLAY blinking text which can be disabled when the user taps is the screen.
Answer by Otavio_ · Dec 28, 2018 at 07:23 PM
There is a much easier way, i'll explain quickly:
create a button, and write you text there and set the image transparency to 0;
in "On click" function of the button in inspector, you put the same button and select GameObject > SetActive and un-check the box;
then, all you have to do is an animation that keep changing the transparency of the text from 0 to 255
click in the button > inspector > image (script) > color > A = 0
Answer by 3kWikiGames · Dec 26, 2018 at 10:57 PM
There's several ways you could do this, if you want the text to completely disappear during blinking then you could either create an animation that loops on and off until the player clicks which may be much simpler than writing a code with a timer, then create a method that turns the sprite renderer on and off between certain intervals based on the timer. EDIT: I've also included the edited code for easier viewing below.
public class TapToPlay : MonoBehaviour {
public float timer;
public GameObject Tap;
public static bool activated = false;
void exit()
{
if (Input.touchCount > 0 && activated == false)
{
Tap.SetActive(false);
activated = true;
}
}
void Update()
{
exit();
timer = timer + Time.deltaTime;
if(timer >= 0.5)
{
GetComponent<Text>().enabled = true;
}
if(timer >= 1)
{
GetComponent<Text>().enabled = false;
timer = 0;
}
}
}
Actually I figured out thee Blinking part but the thing is that I don't know how should I disable the text when the user clicks the screen. It's basically like TAP TO PLAY (constant blinking) when user clicks the screen i.e (Input.Get$$anonymous$$ouseButtonDown(0)) the text (TAP TO PLAY ) should not be shown.
I guess if you had to you could add a bool at the top and so have it like:
private bool activated = false;
if(input.Get$$anonymous$$ouseButtonDown(0)){
if(activated == false){
text.SetActive(false) //or whatever else you would need to do
activated = true;
}
That would allow you to reactivate the text when you need to, but also leave it unavailable in case you don't. Not sure if that's exactly what you're looking for but let me know.
public class TapToPlay : $$anonymous$$onoBehaviour {
public float timer;
public GameObject Tap;
void exit()
{
if (Input.touchCount > 0)
{
Tap.SetActive(false);
}
}
void Update()
{
timer = timer + Time.deltaTime;
if(timer >= 0.5)
{
GetComponent<Text>().enabled = true;
}
if(timer >= 1)
{
GetComponent<Text>().enabled = false;
timer = 0;
}
}
}
This is the code that I used it successfully blinks tap to play . But it doesn't go away when the game starts (when there is a touch input).
Try this and see if that works, may not be a very advanced way of coding it but it should work. To reactivate the blinking text in any other script just write TapToPlay.activated = false; Tap.SetActive(true) and set the touch count back to 0.
public class TapToPlay : $$anonymous$$onoBehaviour {
public float timer;
public GameObject Tap;
public static bool activated = false;
void exit()
{
if (Input.touchCount > 0 && activated == false)
{
Tap.SetActive(false);
activated = true;
}
}
void Update()
{
exit();
timer = timer + Time.deltaTime;
if(timer >= 0.5)
{
GetComponent<Text>().enabled = true;
}
if(timer >= 1)
{
GetComponent<Text>().enabled = false;
timer = 0;
}
}
}
Tried that code but still doesn't work the text doesn't stop displaying after the input.
$$anonymous$$ake sure you have the exit() method within the update, otherwise it wont activate it unless you're activating it with a button. If that's the case you would need to make exit a public void method.
Answer by upasnavig90 · Dec 27, 2018 at 06:00 AM
For blinking the text you can also play with alpha of the color in text, or changing color in animation whatever you want.
and if "Tap to play" is ui button then you can use onbuttonclick event. and simply disable the text or i will suggest to make it interactable false instead of disabling. just a suggestion. :)
and if "Tap to play" is not a ui button than go with the code suggested by @3kWikiGames
Thanks