for two days iv’e been trying to setup simple in game menus in a 3d game (like escape directory, character view menu, inventory).
Iv’e watched about 20 videos now trying to make this work, and im getting very frustrated. The main thing I’m trying to do is just enable/disable a select Canvas when I striek a key in game (escp, c, i) because in my mind that’s the easiest way to do it. One help thread I saw suggested I not disable anything as it will cause issues. So I tried doing it some other ways I’d rather not get into but none of them seem to work.
At one point I was able to close a menu through keystrike if I manual opened it with the unity tool. but otherwise, I cannot get any of the menues to open.
So I guess my question is, can I get an example of how to properly enable an object through key strike?
if (Input.GetKeyDown(KeyCode.C))
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
something.plshelp.enabled(true);
}
public GameObject canvas1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.C))
{
canvas1.GetComponent<Canvas>().enabled = true;
}
}
}
Drag the canvas from the hierarchy to the canvas1 variable, or you could just make a Canvas variable and drag the object on there so you didn’t need get component I think. Put the script on the camera or something.
1 Like
okay I made a new temp project to just try and get the hang of this and its still giving me issues
I found that enable/disable will cause issues, so im trying to use SetActive now. still wont work properly.
public Canvas canvas;
// Use this for initialization
void Start ()
{
canvas.gameObject.SetActive(false);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.C))
{
turnon();
}
}
void turnon ()
{
canvas.gameObject.SetActive(true);
}
with this simple script, the game starts, the canvas goes away. I strike c, nothing happens
A Canvas is a component. If you want to do that, use the game object like I posted and set it to active, don’t get the Canvas, just set the object.
1 Like
fire7side:
public GameObject canvas1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.C))
{
canvas1.GetComponent<Canvas>().enabled = true;
}
}
}
Drag the canvas from the hierarchy to the canvas1 variable, or you could just make a Canvas variable and drag the object on there so you didn’t need get component I think. Put the script on the camera or something.
Holy hell. yes that worked. thankyou so much for just writing that out for me. Every video had a bunch of horrible work arounds that just refused to actually work. I’m new to this and didn’t know the structure for ().enabled so I never would have gotten it.
There’s a pretty simple process you can use to achieve the desired results.
Let’s say you’re working on a game menu with a few screens - Main Menu, Settings, Character Selection.
Create a new canvas
Select the canvas, create an empty game object and name it “MainMenuGO”
Select the canvas, create an empty game object and name it “SettingsMenuGO”
Select the canvas, create an empty game object and name it “CharacterSelectionMenuGO”
Attach a panel and some UI text to each screen so you can distinguish which one is which.
Create an empty game object (not attached to the canvas) and name it “MenuManager”
Select the MenuManager gameobject and create a new script (“MenuManager.cs”)
Now, what you need to do is reference the gameobjects holding the different screens
public GameObject MainMenuGO;
public GameObject SettingsMenuGO;
public GameObject CharacterSelectionMenuGO;
Drag the appropriate UI GameObjects into their slot within the script (use the inspector)
Now, if the user does something you can enable/disable the renderer for that GO
// disable main menu screen
MainMenuGO.SetActive(false);
// enable settings screen
SettingsMenuGO.SetActive(true);
Additionally, you should use Input.GetKey in most cases as it allows you to offer the user keybind changes.
if (Input.GetKey("Escape")) {
MainMenuGO.SetActive(false);
PauseMenuGO.SetActive(true);
}
public GameObject MainMenuGO;
public GameObject Menu2GO;
void Update() {
if (Input.Key("Escape")) {
MainMenuGO.SetActive(false);
Menu2GO.SetActive(true);
}
}
1 Like
ClumsyLobster:
There’s a pretty simple process you can use to achieve the desired results.
Let’s say you’re working on a game menu with a few screens - Main Menu, Settings, Character Selection.
Create a new canvas
Select the canvas, create an empty game object and name it “MainMenuGO”
Select the canvas, create an empty game object and name it “SettingsMenuGO”
Select the canvas, create an empty game object and name it “CharacterSelectionMenuGO”
Attach a panel and some UI text to each screen so you can distinguish which one is which.
Create an empty game object (not attached to the canvas) and name it “MenuManager”
Select the MenuManager gameobject and create a new script (“MenuManager.cs”)
Now, what you need to do is reference the gameobjects holding the different screens
public GameObject MainMenuGO;
public GameObject SettingsMenuGO;
public GameObject CharacterSelectionMenuGO;
Drag the appropriate UI GameObjects into their slot within the script (use the inspector)
Now, if the user does something you can enable/disable the renderer for that GO
// disable main menu screen
MainMenuGO.SetActive(false);
// enable settings screen
SettingsMenuGO.SetActive(true);
Additionally, you should use Input.GetKey in most cases as it allows you to offer the user keybind changes.
if (Input.GetKey("Escape")) {
MainMenuGO.SetActive(false);
PauseMenuGO.SetActive(true);
}
public GameObject MainMenuGO;
public GameObject Menu2GO;
void Update() {
if (Input.Key("Escape")) {
MainMenuGO.SetActive(false);
Menu2GO.SetActive(true);
}
}
https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
https://docs.unity3d.com/ScriptReference/Input.GetKey.html
this was alot more extensive than I was asking for, but I will certainly refer back to this if I run into any future issues.
I updated the code of my project to use “canvas”.GetComponent().enabled = true/false;
everything is now working great. cannot stress how thankful I am for the answers.