- Home /
enabled = true not always working.
I have 3 components on a gameObject. In their Awake() I set their enabled to false, as to ensure they are disabled by default (when they spawn) in my game. This works as expected. Then, in one of those components' Awake(), after the enabled = false; I call a method on all 3 components which looks like this:
public virtual void OnAttach()
{
enabled = true;
print(enabled);
ModelInstance = (GameObject)Instantiate(ModelAsset, transform.position, transform.rotation);
ModelInstance.transform.localScale = gameObject.transform.localScale;
ModelInstance.transform.parent = gameObject.transform;
}
I know the method IS in fact being called on each component, as their models ARE being instantiated and the print() IS printing output. Now as you can see in the code above, immediately after enabled = true;, I print the value of enabled. The output is as follows: True False False
The first component (the one calling OnAttach() on the other 2 components) maintains enabled = true. However, the other 2 components for some reason don't change their enabled to true, and instead stay false.
I've never experienced an issue as mind blowing as this one... I assume something in the enabled property is ignoring my enabled = true but I have no idea why. It can't be anything else in my code since I print the value of enabled literally right after I set it. Yet the printed value is not what I set it to.
I have no idea how to go about debugging this on my own. What could the issue possibly be?
I suspect something like this:
ComponentA calls Awake, enables ComponentB and ComponentC
ComponentB calls Awake, disables itself
ComponentC calls Awake, disables itself
If these are different script classes, Unity does allow you control the execution order.
If they're all three objects of the same class, you need some way to control the order of your calls. The obvious choice is to move the re-enable stuff to Start().
I have the same problem with one particular component, so far I'm just enabling it twice :)
rutter is correct, the enabled status is conflicting between the three scripts. putting the "re-enable" method under start will solve your problem.
That, or have 1 script's awake control the other scripts enabled status. That one script can act as a "manager" and turn off all the scripts, then turn them all back on
I'd move the enable and disable code to Start() ins$$anonymous$$d of Awake(), but the problem is I have code in other components' Start() methods that rely on the code from Awake() to already have been executed.
I spent all morning trying to figure out how to control component execution order but had no luck, but what rutter posted was exactly what I was looking for. Only having Awake and Start to work with was extremely limiting, and OnEnable wasn't making much sense either..
Thanks for the help guys, hopefully the script execution order settings can do what I need it to.
EDIT: Yeah this is what I was afraid of. If I add a base class to execution order, derived classes aren't affected. Having to manually add every derived class to the appropriate location in execution order is really horrible workflow. If I could do it in code somehow, when I'm actually writing the derived class, that'd be much better.. How would I go about doing this? Or is there a better solution?