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

Bug New input system - Inputs not active after loading scene

Discussion in 'Input System' started by Erenquin, Aug 30, 2023.

  1. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Hello,
    I have 3 scenes: Main Menu, Loading, Game.

    If I run the Game scene on its own, inputs are working fine (player can move and fire).
    If I start the Main Menu, and load the Game additively, inputs are working fine.
    If I start the Main Menu, load the Loading scene additively, which itself loads the Game, then the player inputs are not working.
    The Loading scene waits for player input (Fire) to start the Game. This works in the 3rd case. So inputs on the Loading scene are working fine, but not on the Game scene afterwards.

    Also, if I run the Game alone, inputs are working even if I de-activate the "eventsystem" object, which I suppose is only for UI.

    I have already searched and found different posts on similar issues, but do not see the solution for my problem.
    Also those posts usually are about UI inputs not working which is not the problem I have.

    The Main menu is calling that method on Start button click:
    Code (CSharp):
    1.     public void PlayGame()
    2.     {
    3.         SceneManager.LoadScene(loadingScene.sceneId, LoadSceneMode.Additive);
    4.     }
    The level loader is doing this.
    The Raised even only deactivates the camera in the Main menu.
    StartGame is called when the player performs the Fire input.

    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         StartCoroutine(LoadYourAsyncScene(levelToLoad.sceneId));
    5.     }
    6.  
    7.     public void StartGame(InputAction.CallbackContext context)
    8.     {
    9.         SceneManager.UnloadSceneAsync(loadingScene.sceneId);
    10.         Time.timeScale = 1.0f;
    11.     }
    12.  
    13.     private void LevelLoaded()
    14.     {
    15.         loadingText.SetActive(false);
    16.         readyText.SetActive(true);
    17.         levelLoaded.RaiseEvent();
    18.     }
    19.  
    20.     IEnumerator LoadYourAsyncScene(int levelSceneToLoad)
    21.     {
    22.         // The Application loads the Scene in the background as the current Scene runs.
    23.         // This is particularly good for creating loading screens.
    24.         // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
    25.         // a sceneBuildIndex of 1 as shown in Build Settings.
    26.  
    27.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(levelSceneToLoad, LoadSceneMode.Additive);
    28.  
    29.         // Wait until the asynchronous scene fully loads
    30.         while (!asyncLoad.isDone)
    31.         {      
    32.             yield return null;
    33.         }
    34.  
    35.         LevelLoaded();
    36.     }
     
  2. Erenquin

    Erenquin

    Joined:
    Apr 9, 2018
    Posts:
    164
    Found out a workaround.
    I realized that de-activating and re-activating the player input component on my player makes the input system work.
    So I set some events that do just that.
    In StartGame I raise a GameStarted event which then calls InitializePlayer() set on the player object.
    Setting to false, then true, is necessary even if the Player input component is false by default in the scene.

    Code (CSharp):
    1. public class Initialize : MonoBehaviour
    2. {
    3.     private PlayerInput playerInput;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         playerInput = GetComponent<PlayerInput>();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.    
    15.     }
    16.  
    17.     public void InitializePlayer()
    18.     {
    19.         // Necessary to get the player inputs.
    20.         playerInput.enabled = false;
    21.         playerInput.enabled = true;
    22.     }
    23. }