- Home /
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;
}
}
}
}
This is the printed result. It looks like Update stops being called. It is showing 0 calls/sec
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.
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.
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.
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.
good catch. I have the closing curly brace in my actual code.Just forgot to put it here.
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 ^^.
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
Follow this Question
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