Weapon Switching: When I press '1' on keyboard, I can't move or shoot

I tried removing the first weapon to see if that was causing the issue, and therefore only had 2 weapons in the Weapons empty game object(my 2nd and 3rd weapons). My 2nd weapon in the list worked fine, but when I pressed 1 to switch to the 1st weapon in the list, movement and shooting wouldn’t respond again. I pressed 2 to switch back to the 2nd weapon, and it all worked again. Therefore, It isn’t my weapon game object that’s causing the issue, and I can’t find anything on google even remotely close to my issue.

Edit: the issue is similar with the mouse scroll switcher: only the first child in the list locks the screen.

Hi, @izzy.
So we can help you better, can you specify which course and lecture that you are on?

Yes, sorry. Complete Unity 3D, Zombie Runner, Player Input to Select Weapon

Hi izzy,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Yes I have. I put Debug.Log’s on the weapons and it detects the change fine. I’m not getting any error messages and I’ve compared to the lecture code. I also played the next lecture to see if it was a known issue, and added the reset weapon loading delay to OnEnable, but that didn’t fix it either.

Here’s my code and hierarchy.

@Brian_Trotter @Nina any ideas?

Your screenshots look fine to me. At least, I’m not able to spot any problem.

Where did you add the Debug.Logs? I cannot see them in your screenshot.

Does the mechanism get stuck when you select a specific weapon? Or does it happen with all weapons? What happens if you remove the weapons from your Hierarchy and add the same prefab three times? I’m wondering if the problem is caused by the weapons themselves or by something else.

You could also test the mechanism with two weapons only or one only.

Last but not least, the selection of the weapon and the activation are two separate things that don’t have anything to do with each other. Since the code depends on references (currentWeapon), I’m wondering if a wrong reference gets assigned while you are pressing the keys to select another weapon.

At the moment, I don’t know what might be causing the problem, so it is difficult to do research on it. I also do not remember if I ever encounter a problem like yours, so try to figure out more.

By the way, which version of Unity do you use?

Sorry, I removed the Debug Logs because I didn’t see anything beneficial from them. I put them back in to show you.

It isn’t a specific weapon. If I start the game with weapon 0 set as the active weapon, it works fine. If I switch to weapon 1, it works fine. If I then switch back to weapon 0, it switches but breaks input. Then switching to weapon 1 switches fine but the input is broken. Then switching to weapon 2 fixes everything.

I deleted all the weapons in the hierarchy and readded only weapon 2 prefab but 3 times, it had the exact same issue.

I tested the switcher with only 2 weapons, still the first weapon would break.

I am using Unity 2022.3.7f1 and the new input system. Here is a link a video of the issue occurring.

Hm, very odd. :confused:

Could you please fill out our form and send me your zipped project (without the Temp and Library folders)? Don’t forget to add a link to this thread.

When you are done, drop me a line here, so I can check our server.

I filled out the form with the link and my zipped project. My file was really big so I ended up duplicating the project and removing the full level 1(with the forest and goblin camp and whatnot) and left it at the play testing level with the starter assets environment. It was only after that I realized my .git folder was causing the size issue, so I removed the repo from the duplicated project and it zipped small just fine. But it still has the same issue in that play testing scene so you should be able to replicate it just fine.

Thank you for making the project smaller. My computer is rather slow. :slight_smile:

I’ve just downloaded and tested your game, and I was able to recreate the problem in Unity 2023.1.14f1. I was not able to spot any problem, so I tried to identify a pattern. I focussed on the input via keyboard and ignored the mouse wheel.

Interestingly, weapon 1 (= the 2nd weapon) gets disabled only if I press 1-2-3-2. With 1-2-3-1-2 (after restarting the game), weapon 1 continues to work, so my best guess was that the weapon breaks if I go ‘back’. With 3 weapons, selecting weapon 0 again is always ‘back’ in regards to the sequence 0-1-2 (keys: 1-2-3).

Based on this, I checked the algorithm because I supected that the problem might be related to the indices. If 1-2-3-1-2 does not break the weapon at index 1 (key 2) but 1-2-3-2 does, maybe the code iterates over the weapons in an ascending order.

The first thing I did was to replace the two if with else if in the ProcessKeyInput() method. This does not have anything to do with the problem but it is good practice. If you used if on purpose, ignore this part.

Since the only code that iterates over the weapons is SetWeaponActive(), I suspected that there might be a problem with the activation/deactivation of a weapon. For testing purposes, I simply added another loop in which I deactivated all weapons:

void SetWeaponActive()
    foreach (Transform weapon in transform)

    int weaponIndex = 0;

    foreach (Transform weapon in transform)
        if(weaponIndex == currentWeapon)
            Debug.Log("set active weapon");

This fixed the problem for me.

Unfortunately, it is already late here, and I probably won’t have time on Monday and Thursday to check anything. However, I wanted to give you a quick workaround, so you will hopefully be able to continue working on your project.

Since deactivating all weapons solved the problem (for me), I assume that my guess was right. Of course, the two loops are not ‘beautiful’ but I’m sure you’ll be able to refactor the code yourself. By checking the values of the involved variables, you’ll probably also be able to figure out if my guess was right.

Your game looks promising, by the way. Keep it up! :slight_smile:

See also:

Thank you so much, this solved the problem! I’m not sure how to make it more ‘elegant’ than the two foreach loops, but I’ll noodle on it a bit.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms