Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 taxox24335 · Feb 05, 2020 at 12:28 AM · keyboardlabelupdate functionkeycode

If, GetKeyDown, and Update() not working

I'm using Visual Studio to compile this and i'm not sure why it isn't working. If I remove the GetKeyDown statement, the bool is always true and it works fine. As soon as I add it, the bool wont toggle and the float doen't increase. The label prints fine, the bool doen't change with the keypress. I have included more additional floats as a way to see whats functioning and whats not. I can see the number of calls/min of Update and FixedUpdate. The other floats outside of the conditional statements also stop changing when I add the GetKeyDown function. Can anyone help me out?

 using UnityEngine;
 using System.Collections;
 
 namespace menu
 {
     public class testmain : MonoBehaviour
     {
         private float updateCount = 0;
         private float fixedUpdateCount = 0;
         private float updateUpdateCountPerSecond;
         private float updateFixedUpdateCountPerSecond;
         private float stuff;
         private bool statsUp;
         private void Start()
         {
 
         }
 
         private void Awake()
         {
             stuff = 1;
             StartCoroutine(Loop());
             statsUp = true;
         }
 
         private void Update()
         {
             updateCount += 1;
             if (statsUp)
             {
                 stuff += 1f;
             }
             if (Input.GetKeyDown(KeyCode.Insert)) statsUp = !statsUp;
         }
 
 
         private void FixedUpdate()
         {
             fixedUpdateCount += 1;
         }
 
         private void OnGUI()
         {
             //x, y, width, height
             GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label"));
             fontSize.fontSize = 24;
             GUI.Label(new Rect(100, 100, 200, 50), "Update Calls/Sec: " + updateUpdateCountPerSecond.ToString(), fontSize);
             GUI.Label(new Rect(100, 150, 200, 50), "FixedUpdate Calls/Sec: " + updateFixedUpdateCountPerSecond.ToString(), fontSize);
             GUI.Label(new Rect(100, 300, 400, 50), "statsUp + stuff2 : " + statsUp.ToString() + " : " + stuff.ToString(), fontSize);
         }
 
         IEnumerator Loop()
         {
             while (true)
             {
                 yield return new WaitForSeconds(1);
                 updateUpdateCountPerSecond = updateCount;
                 updateFixedUpdateCountPerSecond = fixedUpdateCount;
 
                 updateCount = 0;
                 fixedUpdateCount = 0;
             }
         }
     }
 
 }


alt text

This is the printed result. It looks like Update stops being called. It is showing 0 calls/sec

94h0yxglms.png (9.7 kB)
Comment
Add comment · Show 2
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 Bunny83 · Feb 05, 2020 at 02:36 AM 0
Share

Well, first of all you do not compile your code with Visual Studio unless you actually build a seperate DLL file which you import manually into your Unity project. Scripts inside your Unity project are compiled by the Unity editor when you switch back to the Unity editor. Any warnings or compiler errors that Visual Studio might show you are completely irrelevant. Only the errors and warnings you get in the Untiy console are relevant.


Your code still has a small typo so it would not compile. Again are you 100% sure about your observations? How many times did you test what you claim happens? The Get$$anonymous$$eyDown line will have no effect at all unless you press the insert key on your keyboard. I know this fact with 100% certainty. That's because that's the fundamental basics of how computers and Unity work. So if you have trouble with your code you either did not test it properly or there's still something missing in your code.

avatar image taxox24335 Bunny83 · Feb 05, 2020 at 03:01 PM 0
Share

I am making an external DLL to incorporate in to the project. The code compiles fine, zero errors or warnings in VS. What typo do you see? This is a direct copy/paste from the project. When I press insert, nothing happens. When I add the Get$$anonymous$$eyDown function, the update "counters" stop working. It seems like the entire update function stops working once I add Get$$anonymous$$eyDown.

2 Replies

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

Answer by taxox24335 · Feb 05, 2020 at 03:16 PM

I figured it out. The referenced unityengnie.dll was somehow not working properly. I re-downloaded and reinstalled unity and it works. Pressing insert now increments the float. Now I can get to making a menu. Thanks for the replies.

Comment
Add comment · 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
0

Answer by unity_ek98vnTRplGj8Q · Feb 05, 2020 at 01:36 AM

Is this your exact code? If so you are missing a '}' to close the update function.

Comment
Add comment · Show 3 · 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 taxox24335 · Feb 05, 2020 at 01:58 AM 0
Share

good catch. I have the closing curly brace in my actual code.Just forgot to put it here.

avatar image Bunny83 taxox24335 · Feb 05, 2020 at 02:08 AM 0
Share

I don't see any issue with the code you posted it here except that it's horribly formatted. If you already forgot to post a curly bracket here, are you 100% sure that your code is exactly as yo showed it here? If not you may have excluded an important detail.


Another general advice is: You shouldn't make any Unity callback public. There's no point in making them public as they generally shouldn't be called manually. $$anonymous$$aking them public just opens up for all sorts of hard-to-track-down issues.


ps: Don't you use VisualStudio to edit your script(s)? If so how did you actually manage to screw up the formatting like that ^^.

avatar image taxox24335 Bunny83 · Feb 05, 2020 at 02:19 AM 0
Share

Yes I do use VS. I updated the actual code in the op. I don't see anything wrong either, im kinda stumped. I have included more floats and another function now. This way I can see the number of calls/$$anonymous$$ of Update and FixedUpdate. The other floats outside of the conditional statements also stop changing when I add the Get$$anonymous$$eyDown function.

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

122 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use event.KeyCode 1 Answer

Stimulate key press via a GUITexture 0 Answers

i can assign all keys but the shift any ideas 4 Answers

KeyCode to Ui ? for mobile 1 Answer

detect label click or custom keyboard 3 Answers


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