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

Script component disables itself for no reason

Discussion in 'Editor & General Support' started by Christof_Cheil, Mar 10, 2016.

  1. Christof_Cheil

    Christof_Cheil

    Joined:
    Mar 10, 2016
    Posts:
    6
    Hello everyone,

    i have a problem with a script that disables itself for no reason. The script works fine on another Prefab.
    If i attach it to a Gameobject it is disabled the moment i hit play. What am i doing wrong?

    The script contains references to animations, other gameobjects as well as strings and ints. Not all references point to existing objects. But as long as the script compiles and runs there should be no reason to disable itself. I hope i am missing something that is easy to fix.

    Thanks in advance

    Christof
     
  2. Tanel

    Tanel

    Joined:
    Aug 31, 2011
    Posts:
    508
    Probably some checks in the script for null references and set to disable itself if something doesn't exist.
     
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,960
    You could search in project files to find if any scripts has ".enabled" and go one by one and see if one might be disabling it.
     
    sean244 likes this.
  4. Christof_Cheil

    Christof_Cheil

    Joined:
    Mar 10, 2016
    Posts:
    6
    Thank you Tanel, thank you dogzerx2,

    I did what you suggested, but it didn´t help. But i solved it :) Here Is my solution:

    In "Awake" i had this:

    animator[Animation_BlendIn].layer=2;
    animator[Animation_BlendOut].layer=2;

    When i commented it out it worked. The purpose of putting them in layer 2 was to make it always appear on top of everything. Nothing else is in layer 2. But i don´t know why it deactivated the script. So i probably am not fully aware of the layer concept. If someone knows why this happened please let me know.

    Many greetings from Germany,

    Christof.
     
  5. coronepan

    coronepan

    Joined:
    Jun 13, 2017
    Posts:
    1
    i had an object already placed on the field
    then when i use awake it starts the script disabled ,,
    so i remove awake and just use void start and it works fine
     
  6. Krytose

    Krytose

    Joined:
    Jan 17, 2019
    Posts:
    1
    @coronepan - Thanks! This solved my issue while trying to connect Amazon's DynamoDB.

    Was giving me this error until I removed my awake() method:

    "Exception: Main thread has not been set, is the AWSPrefab on the scene?"
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    For the archives: Unity will disable a script if it throws an exception in Awake().

    So, if you find a script mysteriously disabled when you run your game, find and fix whatever errors it's tripping over in its Awake method, and the problem will go away.
     
  8. Pawciu

    Pawciu

    Joined:
    Jun 21, 2015
    Posts:
    15
    Yes, those error does not show in Unity Editor Console for me. I've added Assert in Awake and Start method in my MonoBehaviour so I can see the stack trace in Console window this way.


    Code (CSharp):
    1. using UnityEngine.Assertions;
    2.  
    3. ...
    4.  
    5. private void Awake()
    6. {
    7.     Assert.IsTrue(this.enabled);
    8. }
    9.  
    10. private void Start()
    11. {
    12.     Assert.IsTrue(this.enabled);
    13. }
     
  9. rtcheteden

    rtcheteden

    Joined:
    Dec 30, 2020
    Posts:
    21
    SOLVED i didnt check my console for errors and there were no compile errors so i didnt know what was going on, coming here i found @coronepan 's comment and i replaced my awake method with a start, it worked but i wanted to use the awake method and didnt know why the start method worked and awake didnt, then i saw @JoeStrout 's comment and looked in the console after running it again, i hadnt manually put in a null reference exception so i wasnt expecting anything, but upon further investigation into the report i saw it trying to say i have a null reference exception at line 29, looking in the script i found where i tried incorrectly assigning the post process volume component "camcheck = GameObject.Find("camcheck").transform;" (unity 2020.3) and by commenting/deleting it out, voila
    thanks guys.
     
  10. LagSik

    LagSik

    Joined:
    Apr 6, 2020
    Posts:
    5
    Work for me too. Thanks
     
  11. unity_1stmanleader

    unity_1stmanleader

    Joined:
    Jul 5, 2021
    Posts:
    3
    Thanks a lot, guys! Solving the null returns on Awake did the trick!
     
  12. Murphy219

    Murphy219

    Joined:
    Oct 22, 2021
    Posts:
    2
    Just some info for anybody else with this problem that hasn't been able to fix it. Try this video. It might just be a script execution order issue that can be fixed in the settings.

     
  13. LouWorldOrder

    LouWorldOrder

    Joined:
    Oct 24, 2022
    Posts:
    14
    I had this issue in one of my scenes, that I had made when I was A LOT more green behind the ears.

    And basically... I just was not using the object's proper name, when using GameObject.Find. The examples/tutorials that I was following all had objects that had the exact same name as the script we were looking for, whereas in my scene, the game objects had descriptive names, but not exactly the same as the script on the objects!

    I was also 'finding' them in Awake, which caused Unity to deactivate the script when I hit play.
     
  14. mmvlad

    mmvlad

    Joined:
    Dec 31, 2014
    Posts:
    98
    If this expected behaviour? If yes, this is complete madness.

    I believe in older versions everything worked fine, exceptions in Awake() were thrown like they should be.
     
    JoeblizzPizza likes this.
  15. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    The exception is thrown as per usual (and winds up in your console). Nothing weird happens there.

    It seems reasonable for the component to be disabled if it couldn't be created correctly.
     
    JoeblizzPizza likes this.
  16. petercseh36

    petercseh36

    Joined:
    Jan 27, 2023
    Posts:
    1

    Thank you so much for your answer. This was the solution for me.
     
  17. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,994
    Is that a desirable behavior? Seems like a bug to me. Logged as IN-40572