- Home /
Timescale help
I'm making a game where the user taps to start and I have used the script below which works fine, but I have text fading in and out saying "tap to start". The text doesn't show up because the timescale is 0. Does anyone know how to just make the sprite show up but make the rest still frozen? I would really appreciate it.
Script:
using UnityEngine;
using System.Collections;
public class TapToStart : MonoBehaviour {
void Start () {
Time.timeScale = 0;
}
void Update () {
if(Input.GetMouseButtonDown(0))
{
Time.timeScale = 1;
}
}
}
As far as I know timeScale is global so you can't set it individually for different scripts/ An alternative might be to create your own global timescale variable which you multiply everything you want paused by. Then you can set that to 0 to effect everything except specific things.
Answer by HarshadK · Aug 19, 2014 at 02:04 PM
You can use GUI.Label for your text in this case since OnGUI elements are independent on the timescale and will work even if timescale is set to zero.
And for flashing effect you would need to implement your custom timer which will be independent of timescale (like using system time).
A simple Google search will provide enough information and code snippets.
Thanks for the suggestion, it works, but do you know how to make the GUI Label to fade in and out as my sprite did, and then destroy it when the screen is tapped/mouse is clicked?
Hey Shay! how were you changing the alpha of your old text because you can use Time.realtimeSinceStartup to get a time value to base your alpha on. Here's some code on how you could use it with a GUI.Label as Harshad$$anonymous$$ suggested:
//Unityscipt (~javascript)
private var i : int = 0;
var speed : int = 255;
var str : String = "This is your text";
private var colorStr : String;
function Update () {
i = $$anonymous$$athf.PingPong($$anonymous$$athf.FloorToInt(Time.realtimeSinceStartup*speed), 255);
colorStr = "<color=#ffffff" + i.ToString("x2") + ">";
}
function OnGUI(){
GUI.Label(Rect(0, 0, 1000, 500), colorStr + str + "</color>");
}
Scribe
This was the fading code I was using on my sprite:
using UnityEngine;
using System.Collections;
public class Fade : $$anonymous$$onoBehaviour {
public float duration = 1.0f;
public SpriteRenderer sprite;
void Update() {
float a = $$anonymous$$athf.PingPong (Time.time / duration, 1.0f);
sprite.color = new Color(1f, 1f, 1f, a);
}
}
How would I use your script in csharp? I suck at translating from javascript...
If you are using the most up-to-date version of unity then you can just change line 10 Time.time to Time.unscaledTime
Your answer
Follow this Question
Related Questions
Time.time explanation. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to pause time when hit an object 0 Answers
Physics behave weirdly after timeScale change 1 Answer
2D movement stutter 4 Answers