- Home /
Show FPS when key press
Hi im using the FramesPerSecond.js script supplied here: http://wiki.unity3d.com/index.php?title=FramesPerSecond -
where would i put a toggle command? (like press 'z' to turn it on and off)
Answer by Invertex · Jan 10, 2014 at 12:58 AM
Create a bool, that's something like fpsEnabled.
Then, wrap everything in the Update function from that script with an:
if(fpsEnabled)
{
//do all that stuff that's in the FPS Update function
}
Then we also:
//clear the text if fpsEnabled untrue
//so we aren't left with the last FPS number sitting onscreen
else
{
guiText.text = "";
}
And add another if statement at the top of your Update(), it is your if(Input.GetKeyDown("whatever"))
. And inside that one, toggle the fpsEnabled bool by using fpsEnabled = !fpsEnabled;
.
if(Input.GetKeyDown("p"))
{
fpsEnabled = !fpsEnabled;
}
When you format your bool update like this, it works as a toggle each time it's called. So if it's true, it will become false, and if false it will become true. Simple and clean :)
hold up i got an error
var updateInterval = 0.5;
private var accum = 0.0; // FPS accumulated over the interval
private var frames = 0; // Frames drawn over the interval
private var timeleft : float; // Left time for current interval
private var fpsEn : boolean = false;;
function Start()
{
if( !guiText )
{
print ("FramesPerSecond needs a GUIText component!");
enabled = false;
return;
}
timeleft = updateInterval;
}
function Update()
{
if(Input.Get$$anonymous$$eyDown("p")
{
fpsEn = !fpsEn;
}
if(fpsEn)
{
timeleft -= Time.deltaTime;
accum += Time.timeScale/Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if( timeleft <= 0.0 )
{
// display two fractional digits (f2 format)
guiText.text = "" + (accum/frames).ToString("f2");
timeleft = updateInterval;
accum = 0.0;
frames = 0;
}
}
else {
guiText.text = "";
}
}
is it right?
What was the error? The only real difference from $$anonymous$$e is that I declared the var like this:
public var fpsEn = false;
edit: Nvm, I see your issue. I missed a ")" around the Input.Get$$anonymous$$eyDown("p"), sorry!
Should be (Input.Get$$anonymous$$eyDown("p"))
Assets/Script/FramesPerSecond.js(36,1): BCE0044: expecting ), found '{'.
L36 is
if(Input.Get$$anonymous$$eyDown("p") {<<<<<<< fpsEnabled = !fpsEnabled; }
Well, the error told you right there what was wrong! :P It was expecting an ")", so you have to look for where you're missing one of those. The place being the Get$$anonymous$$eyDown line.
The first number in an error like that, usually tells you what line in the script it is encountering the error. Though technically it isn't line 36, the issue on line 29 still allows the code to continue, thinking the rest is part of 29's statement, so it only errors once it finds out that this argument hasn't been closed properly.
Your answer
Follow this Question
Related Questions
Gun Firing help? 2 Answers
Weapon ("Snapping") Socketing FPS 1 Answer
How to let my gun shoot ?? 0 Answers