Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

New Input System - using 2 different keys to switch between two (items / objects)

Discussion in 'Scripting' started by Welly10, Jan 19, 2022.

  1. Welly10

    Welly10

    Joined:
    Aug 13, 2021
    Posts:
    38
    I'm trying to switch between two items using the new Input System but the items don't instantiate while I'm calling them,,,
    where what I've done so far
    Code (CSharp):
    1. public bool currentWeapon;
    2.  
    3. public void SetWeapon(InputValue value)
    4.         {
    5.             WeaponInput(value.isPressed);
    6.         }
    7.  
    8. public void WeaponInput(bool newWeaponState)
    9.         {
    10.             currentWeapon = newWeaponState;
    11.         }
    12.  
    13.  
    14. // this is another script
    15.  
    16. if(_starterAssetsInputs.SetWeapon(0, 1))
    17.         {
    18.             Instantiate(weaponManager.weapons[0], pivotGun);
    19.             Instantiate(weaponManager.weapons[1], pivotRifle);
    20.         }

    I'm still learning and I'm trying to learn how to use the new Input System

    I've an error when i tried to call (StarterAssetsInputs. wepaons
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Break it apart:

    - gather the correct intent from the user and prove that works (print out that you got the intents when you do them in runtime). Do not proceed until that simple step works.

    Once that works, you know the input system is done and you have collected the user's intention.

    Now:

    - process the given intents and change the state of the game

    Don't attempt to debug these two things in tandem. Put your pants on one leg at a time.

    Beyond those basic rules of solid engineering, you still must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Welly10 likes this.
  3. Deleted User

    Deleted User

    Guest

    Quick and dirty.

    Code (CSharp):
    1. if(Keyboard.current.anyKey.wasPressedThisFrame)
    2. {
    3.     Debug.Log("A key was pressed");
    4. }
    Or invoke events.

    - Select the player Input script, under "behaviour" select Invoke C# Events".
    - Select the Events dropdown
    - Find the movement event.
    - Attach the below script to a gameobject, and drag-n-drop that into the movement event.

    Code (CSharp):
    1.  
    2. public class Example : Monobehaviour
    3. {
    4.     public void Movement(InputAction.CallbackContent context)
    5.     {
    6.         float x = context.ReadValue<Vector2>().x;
    7.         float y = context.ReadValue<Vector2>().y;
    8.  
    9.         Vector2 both = context.ReadValue<Vector2>();
    10.  
    11.     }
    12. }
     
    Welly10 likes this.
  4. Welly10

    Welly10

    Joined:
    Aug 13, 2021
    Posts:
    38
    Thanks you guys for the replies, I've traced the issue and need to change the logic a bit so I can achieve what I needed to do,,, really appreciate your help
     
    Kurt-Dekker likes this.