Holding the “tab” key should bring up a panel, but it doesn’t. I can’t figure it out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TabPanel : MonoBehaviour
{
public GameObject tabpanel;
private void Start()
{
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
bool isActive = tabpanel.activeSelf;
tabpanel.gameObject.SetActive((tabpanel.gameObject.activeSelf)); //need to add this line to make the toggling work
Debug.Log("Key pressed");
}
}
}
Scripts on disabled GameObjects (i.e, ones that are not active) do not run. If this script is on or in your panel, this code is not running when disabled. Put it on another GameObject to control the panel.
line 26 and line 27, contrary to your comment, does not toggle the state. Notably, you don’t use the isActive boolean that you declare. If you want to toggle the Active state of a GameObject, just do this:
Ah ok. Luckily you said that, we can start from there with how to solve the issue. So, what I learned from opening a panel with a button is you need to hide the panel in the “Inspector” so when you click the button once it brings the panel up and then pressing the button again, it hides it.
I thought I could do the same thing with the “tab” key by starting off hiding the panel but it didnt work. After enabling it and using your code, hitting “tab” hides it but hitting the key again doesn’t bring it back
Oh my goodness, right now please take five minutes and get really comfy with the editor. Click everything, try it all out, know it. Under Window menu you can launch any window. Right-clicking on any window tab lets you open any other one. You can have fifty copies of your scene if you want. Also you can use preset layouts in the upper right corner… try them all, customize one, get very familiar with changing them around as you work because otherwise you will only be using Unity with one hand tied behind your back. Know your tools.
When that script hides the popup, WITHOUT stopping the game, press PAUSE in the top center of Unity, and look in your inspector.
At this moment, while the game is running and paused and your window is hidden, is the GameObject where the script is on ALSO disabled?
If so, check my #1 entry in the first post. Move your control script to a GameObject that does not get disabled.
If the GameObject your script is on is still enabled when the popup is hidden, please say so directly and we can eliminate #1 as a possible problem. We still have not eliminated #1.
Not sure if the canvas is a gameObject but thats where my panel is on. And hitting “tab” only disabled the pop-up panel, not the canvas. Hitting “tab” again for some reason doesnt bring the pop-up panel back.
Excellent! Now we have eliminated one possible problem. Once we eliminate every other possibility we will have located the problem. Welcome to engineering!
Next step: when you disable the popup, then re-enable it and it fails to show up, again press PAUSE and now go study the popup window GameObject, ie. the object you dragged into the tabpanel variable.
Look at the tabpanel in the inspector window.
Possible outcomes that we reasonable little engineering monkeys here might expect:
is it still disabled? then your code did not enable it!
is it enabled and yet we can’t see anything still? now we can investigate that differently.
is tabpanel actually just GONE now? that could be interesting too. What made it go away?
some other thing?!
Now if outcome 1 above is the answer, what happens if you tick the box back to enabled in the inspector? Does your panel re-appear? Now if you re-toggle it with your key, does it go away? OR does it just stay enabled?
These are all ways we winnow down the problem when we are engineering. You might think of a few more different things to try too, as you do your engineering. This is actually what it’s all about.
Hello, in first place if you want this open during youre holding tab, must use only “GetKey” not “GetKeyDown”, take this script: (My first question answered!):
public GameObject tabpanel;
bool panelactive;
// Update is called once per frame
void Update()
{
tabpanel.SetActive(panelactive);
if (Input.GetKey(KeyCode.Tab))
{
panelactive = true;
}
else { panelactive = false; }
}
Hello hello,
My usual bid is to try with another key (like B or something) because Escape, tab and so on may be captured by something else
I’m using a one-line method for these :
if (Input.GetKeyUp(KeyCode.B))
{
tabpanel.SetActive(tabpanel.activeSelf ? false : true);
}
Make sure you don’t disable what should be capturing hotkey (I see TabPanel checking for tabpanel GameObject?) If Update is disabled it won’t get your Input.
Is there anything else forcing it to be disabled ? Like disabling it in Update somewhere else? Try to get your updates methods as limited as possible unless you exactly now what does what.
Is static checked?
Your problem can lie somewhere else, so I’m with Kurt-Dekker concerning pausing and analysing the situation as deeply as possible.
When the panel starts disabled, every time I run my program and hit “tab” or “B” when I changed it to B, I get this error “object reference not set to an instance of an object”.
And the error goes right to this line: tabpanel.gameObject.SetActive(!tabpanel.gameObject.activeSelf);