Script component disables itself for no reason

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

Probably some checks in the script for null references and set to disable itself if something doesn’t exist.

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.

1 Like

Thank you Tanel, thank you dogzerx2,

I did what you suggested, but it didn´t help. But i solved it :slight_smile: 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 Likes

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

5 Likes

@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?”

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.

19 Likes

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.

using UnityEngine.Assertions;

...

private void Awake()
{
    Assert.IsTrue(this.enabled);
}

private void Start()
{
    Assert.IsTrue(this.enabled);
}

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.

Work for me too. Thanks

Thanks a lot, guys! Solving the null returns on Awake did the trick!

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.

1 Like

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.

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.

1 Like

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.

1 Like

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

Is that a desirable behavior? Seems like a bug to me. Logged as IN-40572

I guess it works when you wake up, as its name suggests. Because I had such a problem, too, and based on what you explained, I set the malfunctioning objects to be disabled when the game started and enabled them when the game started. It started working properly.

Unity will disable a script if it throws an exception in Awake().

Thanks for explanation!