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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

New Input System Help

Discussion in 'Input System' started by Boniii, Oct 8, 2022.

  1. Boniii

    Boniii

    Joined:
    Jun 20, 2018
    Posts:
    2
    Hello all,

    I'm trying to unspaghetify my code, and I've decided to migrate my input system into using the new input system. I looked at some tutorials, and found that I have to create an instance of the Input Action Asset Class in order to subscribe my methods to its events like so:


    private PlayerInputAction playerInput;

    void Awake()
    {
    playerInput = new PlayerInputAction();
    }

    void OnEnable()
    {
    playerInput.Enable();
    //more subscribing stuff here
    }


    The problem is though, I have several other scripts that need input such as with animation, physics, and secondary player actions. I don't want to combine them into one script either. Is it okay if all of these separate scripts create an instance of the PlayerInputAction class on their own? If not, is there a way for me to be able to subscribe my methods to the events without creating a new instance?

    I apologize in advance if this seems like a silly question, I am quite new to OOP in general. Thanks!
     
  2. Boniii

    Boniii

    Joined:
    Jun 20, 2018
    Posts:
    2
    I realized too late that I put this on the wrong category. Please disregard this post!
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    I'll move your post for you.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    You should have a single script that responds to input actions, and that can either keep internal state and be polled by other scripts or it provides events that other scripts can subscribe to in order to receive input events. Otherwise you run into trouble with enabling/disabling input from different scripts, with bugs like input being disabled because the script that was supposed to enable it was executed before the script that disabled it.
     
    Boniii likes this.