Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Is there a way to assign methods to Inputs with code and not through the inspector?

Discussion in 'Input System' started by brandonholliday1, Feb 1, 2024.

  1. brandonholliday1

    brandonholliday1

    Joined:
    Nov 18, 2022
    Posts:
    2
    Hey all,

    I'm new to the Unity Input system, and I'm not sure I'm using the correct vocabulary to describe my question, so I've attached a GIF in addition to my description to try and better show what I want to do.

    Essentially, I have multiple characters in my scene that the player can switch between. A handful of them (called "Scout" in my hierarchy) all share the same prefab and controls. Which "Scout" the player switches between is quite important to some of the puzzles and the primary gameplay loop. I'm using Invoke Unity Events as the Behavior property of the Player Input component. I find this method of using the Unity input system the easiest to understand. I also discovered that Unity seems to know which Scout the player switches to if I manually assign the methods to the Player Input via the inspector. Which is fine, but I plan to have upwards of dozens of different "Scouts" for the player to switch between. Which means every level and puzzle would become quite tedious having to manually assign them all in the inspector and could lead to some error on my part.

    So I'm wondering: can I assign the GameObjects and their corresponding methods in the Player Input programmatically with code versus having to do it all in the inspector? I have included a GIF that I hope better shows what I'm going for.

     
  2. brandonholliday1

    brandonholliday1

    Joined:
    Nov 18, 2022
    Posts:
    2
    Never mind, I think an easier way is just to have one Player Scout Prefab Variant with the input controller already set up that I instantiate at the location of the Scout you tend to switch into.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,417