Search Unity

Component inexplicably disabling itself on Awake()

Discussion in 'Scripting' started by Ramobo, Jan 2, 2020.

  1. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    212
    It's that simple: I enter play mode and our inventory manager just gets disabled.

    As far as I know, the only way to disable a component is `component.enabled = false;`. I used Ctrl+F to find all occurences of ".enabled" in the solution and not a single one pointed to that component.
    It's tied to that component only, as this is the only affected component and placing it in our main scene instead of DontDestroyOnLoad didn't change anything. Neither did placing multiple instances: all of them got disabled.
    `Awake()` and `OnDisable()` is called as per usual, but it doesn't reach `Start()`.

    This just started happening recently for seemingly no reason and I can't figure out why. My teammate didn't have the issue on the latest commit of our develop branch, so I tested the game on that and it occurred, making me think it was a problem on my end. Now I committed and she has the issue too.

    This is the typical Unity 101 issue at this point, the difference being that deleting Library didn't fix it. Reproduced in 2019.1.14f1 and 2019.3.0f3.
     
    Last edited: Jan 2, 2020
  2. Ramobo

    Ramobo

    Joined:
    Dec 26, 2018
    Posts:
    212
    I traced it back to this method, the last statement of `Awake()`:
    Code (CSharp):
    1. private void InitializeItems()
    2. {
    3.     money = new InventoryItem<int>(() => _savedGameManager.CurrentGame.inventory.money, value => _savedGameManager.CurrentGame.inventory.money = value, this);
    4.  
    5.     hasMasterKey = new InventoryItem<bool>(() => _savedGameManager.CurrentGame.inventory.hasMasterKey,
    6.         value => _savedGameManager.CurrentGame.inventory.hasMasterKey = value, this);
    7.     hasStorageRoomDoorKey = new InventoryItem<bool>(() => _savedGameManager.CurrentGame.inventory.hasStorageRoomDoorKey,
    8.         value => _savedGameManager.CurrentGame.inventory.hasStorageRoomDoorKey = value, this);
    9. }
    I remove that call and it works again. Specifically, `CurrentGame` is null, an issue I can't seem to fix, even with the logical fix of script execution order. In any case, that doesn't explain why it's being disabled.
    `InventoryItem<T>` doesn't touch anything in the Unity API.

    But really, all of this should be irrelevant because there's no `component.enabled = false;` calls pointing to that component.
     
    Last edited: Jan 2, 2020