Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Why is my Input Class not deleted when changing scenes ?

Discussion in 'Input System' started by Lahol, Jul 10, 2022.

  1. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    Code (CSharp):
    1. public class Navigation : MonoBehaviour
    2. {
    3.  
    4.     private PlayerInput pI;
    5.     public int i;
    6.     public GameObject[] options;
    7.  
    8.     // Start is called before the first frame update
    9.     private void Awake()
    10.     {
    11.         pI = new PlayerInput();
    12.         pI.UI.Enable();
    13.         pI.UI.Confirm.performed += enter;
    14.         pI.UI.Return.performed += retour;
    15.         pI.UI.SlideLeft.performed += slideLeft;
    16.         pI.UI.SlideRight.performed += slideRight;
    17.  
    18.     }
    PlayerInput is a generated C# class from the new Input System and Navigation is just a script called on a specific scene. It works well but I noticed that when loading a new scene after this one, these functions are still launched although the Navigation script is not in the new Scene.
    Code (CSharp):
    1.         pI.UI.Confirm.performed += enter;
    2.         pI.UI.Return.performed += retour;
    3.         pI.UI.SlideLeft.performed += slideLeft;
    4.         pI.UI.SlideRight.performed += slideRight;
    I tried to access the GameObject inside one of those functions and got an error (which makes sense because the script is not here) but I can't get why the functions are still running...
    I searched a lot on the forums but I couldn't find anything, so if you guys have any ideas what I could be missing, let me now !
    Sorry for the inconvenience
     
  2. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    Up ! Any help please ?
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How do you know they are "still running"? Don't guess and just go by behavior, find out for sure. I would suggest proper debugging, perhaps with Debug.Log to confirm, and consider using the second parameter to this method to see which object is calling it, in case you have this script attached elsewhere https://forum.unity.com/threads/tips-for-new-unity-users.701864/#post-5057741
     
  4. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    Hello, thank you for answering !
    I just created a new project and put a script (generated C# class from the new Input System) on an Object in the first Scene.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class Hello : MonoBehaviour
    5. {
    6.     private InputTest input;
    7.     private void Awake()
    8.     {
    9.         input = new InputTest();
    10.         input.Enable();
    11.         input.UI.confirm.performed += testT;
    12.     }
    13.  
    14.     public void testT(InputAction.CallbackContext context)
    15.     {
    16.         if (context.performed)
    17.         {
    18.             Debug.Log("still running", this);
    19.         }
    20.     }
    21. }
    I also created a second scene with just a cube (no scripts attached).
    When loading the second scene (async or not), the functions from the script are still launched

    I am using the Input System 1.3.0 - January 05, 2022

    So I I think it's not a bug and maybe that's how it's supposed to work but then is there a way to disable these functions by entering a new scene?

    Thank you in advance !
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I only see one scene in your (small) screenshot and one Debug.Log statement? Where are you changing scenes?
     
  6. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class LevelLoader : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         StartCoroutine(LoadAsynchronously("A"));
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     IEnumerator LoadAsynchronously(string scene)
    14.     {
    15.         yield return new WaitForSecondsRealtime(0.5f);
    16.         AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
    17.     }
    18. }
    This is my script for changing scene, it's just a simple script set on a GameObject in the firstscene.
    I don't know what I am supposed to show you, I just created a simple project with one object and 2 scripts.
    But is it how it's supposed to work or is it a bug ?

    I think the "bug" comes from this part showed earlier:
    input.UI.confirm.performed += testT;

    Could it be possible that it always launch the "testT" function even when the scene changed ?
     
    Last edited: Jul 12, 2022
  7. andrew_oc

    andrew_oc

    Unity Technologies

    Joined:
    Apr 16, 2021
    Posts:
    77
    You should really be calling Dispose on that PlayerInput instance when leaving the first scene, assuming it's one that's generated from the Input System editor. Instances of input assets created this way aren't attached to the scene in any way and will hang around in memory across scene changes. Another issues, although related to the first, is that it's good practice to remove event handlers when you're done with them. If this is the behaviour that you want, then when changing scenes, you should do something like

    Code (CSharp):
    1.         pI.UI.Confirm.performed -= enter;
    2.         pI.UI.Return.performed -= retour;
    3.         pI.UI.SlideLeft.performed -= slideLeft;
    4.         pI.UI.SlideRight.performed -= slideRight;
    It wouldn't be completely necessary if the instance of the PlayerInput class is properly destroyed, but it's a good habit to get into regardless.
     
    Lahol likes this.
  8. Lahol

    Lahol

    Joined:
    May 23, 2022
    Posts:
    15
    Thank you for your help!
    It worked:)