Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thornekey · Jan 10, 2014 at 12:44 AM · javascriptfps

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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 :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thornekey · Jan 10, 2014 at 02:02 AM 0
Share

thanks heaps for your help. i understand now

avatar image thornekey · Jan 10, 2014 at 02:12 AM 0
Share

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?

avatar image Invertex · Jan 10, 2014 at 02:22 AM 0
Share

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"))

avatar image thornekey · Jan 10, 2014 at 02:25 AM 0
Share

Assets/Script/FramesPerSecond.js(36,1): BCE0044: expecting ), found '{'.

L36 is

if(Input.Get$$anonymous$$eyDown("p") {<<<<<<< fpsEnabled = !fpsEnabled; }

avatar image Invertex · Jan 10, 2014 at 02:27 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gun Firing help? 2 Answers

Weapon ("Snapping") Socketing FPS 1 Answer

How to let my gun shoot ?? 0 Answers

UnityEngine has no appropriate version problem 1 Answer

Does Anyone Have a Halo-Like Scripts? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges