How to create a menu in unity to purchase ammo for my weapons - unity3d

I am a student currently creating a zombie FPS game using Unity 3D and I simply want to when I press 'E' on the keyboard, I want a menu to pop up and show the prices of the ammo (I have a pistol9mm and an AK) and when I click on buy, the ammo is purchased and I have more ammo.
Is there any helpful suggestions on how I will approach this scenario please ?

So first and most importantly, you have to create the Menu where you are going to buy the ammo from, make the buttons and then script them in a way that when you press them they take money away from the player and then gives him ammo. A single button "Buy ammo" is enough and just a little panel on which the button should stay is needed. After making the menu work, go to the script where you check if the player has pressed the "E" key. Create a new Game Object which holds the Menu where you buy the ammo from and when checking if the player has pressed the "E" key, check if the menu is opened. If it is not, then open the menu, if it is opened, close it. Here is a simple way of doing it.
public GameObject menu;
private bool isOpen = false;
void Update(){
if(Input.GetKeyDown("E")){ //When "E" key is pressed
if(isOpen == false){ //If the menu is closed, open it
isOpen = true;
menu.SetActive(true)
}else if(isOpen == true){ //If the menu is opened, close it
isOpen = false;
menu.SeyActive(false);
}
}
}
The way it works is, it either sets the menu to active or unactive when you press the "E" key and it's really simple too. Also when you are done with the Menu set it to active so it doesnt apear on the screen when starting the game, just so it will apear after pressing "E" and so players don't have to close it every time.
I hope i helped you, if you have any questions feel free to ask.

Related

Button can only be clicked once (Unity)

I'm fairly new to this.
When I click on the button to rebind the jump key, the rebind works fine. First the panel "Waiting on rebinding..." pops up, then I press the key, I want jump to be, then the new key shows up on the button as the binding.
This works only once though. When I want to click on the same button again, it doesn't allow me to click on it. The button doesn't even change color anymore, when you hover over it.
What I tried to solve this was to duplicate the button. When I click on either of the buttons and rebind the jump key, it works the first time, but not after that. Both buttons are unclickable. Closing and opening the button panel doesn't work either.
Here is the code for the OnClick() on the button:
public void JumpStartRebinding()
{
startRebindObject.SetActive(false);
waitingForInputObject.SetActive(true);
movement.PlayerInput.SwitchCurrentActionMap("UI");
jumpAction.action.Disable();
rebindingOperation = jumpAction.action.PerformInteractiveRebinding()
.WithControlsExcluding("Mouse")
//.WithCancelingThrough("<Keyboard>/escape")
.OnMatchWaitForAnother(0.1f)
.OnComplete(operation => JumpRebindComplete())
.Start();
}
private void JumpRebindComplete()
{
int bindingIndex = jumpAction.action.GetBindingIndexForControl(jumpAction.action.controls[0]);
bindingDisplayNameText.text = InputControlPath.ToHumanReadableString(
jumpAction.action.bindings[bindingIndex].effectivePath,
InputControlPath.HumanReadableStringOptions.OmitDevice);
jumpAction.action.Enable();
rebindingOperation.Dispose();
startRebindObject.SetActive(true);
waitingForInputObject.SetActive(false);
movement.PlayerInput.SwitchCurrentActionMap("Player");
}
The issue doesn't appear to be with your code. I was having this exact same issue a few days ago. It likely has to do with the fact that a UI object is "covering up" the button. A first step I'd take is making sure that nothing is obscuring your button after re-enabling it (often times, TextMeshProUGUI objects with "Raycast Target" enabled are the culprit).

How do I check whether I have changed rooms in Game Maker Studio 2?

So I have been working on a pause menu for a game I have been working on with Game Maker Studio 2.
Whenever I change rooms (like going to the options room or main menu) the pause menu stays and everything looks very weird and overlapped. Is there a way to check if I have changed rooms to destroy the objects the menu is made up of
(So like if (room has changed) {Delete pause menu})*
If someone could give me the correct syntax that would be awesome!
Thanks.
*I have sorted out deleting the menu, just need to know what to put in the if statement
For my game, I've done a check if the room is the same as the room in the main menu/options menu, or make a check that the pause menu only appear when it's not the main menu/options menu.
var roomname = room_get_name(room);
if (roomname != r_mainmenu && roomname != r_optionsmenu)
{
//draw pause menu
}
Another idea is to hide the pause menu once you go back to the main menu.

Buttons in a pause menu created in canvas in unity not working, even though everything is correct

So I have a pause menu created in unity. There is a panel and on it, there are three buttons but they do not seem to be working, meaning I can not click on them. Also, interactable of every button is checked and event system is present in the hierarchy.
Hierarchy image
Following is the code for the resume button:
public void resumegame()
{
levemusic.UnPause();
pausemenu.SetActive(false);
Cursor.visible = false;
gameisPaused = false;
Time.timeScale = 1;
}
Either disable this component on the panel
Or move the panel object such that it is on top of the hierarchy in the object hierarchy

BottomSheetDialogFragment- Close only on full swipe Kotlin Android

I have a BottomSheetDialogFragment to show list of items inside a fragment. Each list item got a click event mapped. The problem here is, most of the times click action turns into swipe down action and BottomSheetDialogFragment is getting closed. I have no custom events to monitor the events in my code.
dialog?.also {
it.findViewById<View>(R.id.design_bottom_sheet)?.let { bottomSheet ->
bottomSheet.layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED
BottomSheetBehavior.from(bottomSheet).skipCollapsed = true
}
}
I have to prevent the close event of BottomSheetDialogFragment on click action which turns into swipe down action because of small extra movement added. This could happen and needs to handle since this application will be used by general public.
Can anyone suggest to find the swipe distance on BottomSheetDialogFragment. So that if the value is lower, the dialog will remain open. And if the value if high because of intentional swipe event, the dialog will get dismissed.

How to create a popUp in unity3d?

I want to create a PopUp for my game, my requirement is to open a popUp when user click a button.
And popUp contains a image for its background, a close button on upper-right corner and two buttons on the popUp (lets say YES & NO).
I make R&D but nothing found relevant.
Any help would be appriciated.
P.S. I don't want to use any third party plugIn Like NGUI, 2D ToolKit etc.
Unity till 4.5
You can build most components with GUITexture wiht the legacy UI system.
http://docs.unity3d.com/Manual/class-GuiTexture.html
Build your background and buttons from textures of the scheme below. For the buttons you also use GUIText and make the clickable/touchable.
For more info see the scripting guide.
http://docs.unity3d.com/Manual/GUIScriptingGuide.html
Unity 4.6 or newer
The GUI system of Unity easily allows you to do that. You will need a Canvas and a Sprite for the background. And two buttons as children for YES and NO.
See the introduction first
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/the-new-ui
The manual shows all components
http://docs.unity3d.com/Manual/UISystem.html
GUI Reference
http://docs.unity3d.com/ScriptReference/GUI.html
Try creating something like this for the pop-up button(JavaScript):
var popupactive : boolean = false;
var (Name of the GUI window) : GUITexture;
var (Picture1) : GUITexture;
var (Picture2) : GUITexture;
var (Picture3) : GUITexture;
(Name off the GUI window).enabled = false;
(Picture1).enabled = false;
(Picture2).enabled = false;
(Picture3).enabled = false;
function OnMouseUp(){
if(popupactive==false){
popupactive = true;
}
else{
popupactive = false;
(Name of the GUI window).enabled = false;
}
}
Then try adding a function that closes the GUI for the exit button and "no" button to close the opened GUIs, Also remember to import the separate scripts assigned to the separate buttons inside the code! Remember also to assign them in the Unity editor. Hope it helps!