Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by PsychoDuckArcade · Aug 21, 2014 at 05:53 PM · not workingvalueenabled

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Aug 21, 2014 at 05:56 PM 2
Share

I suspect something like this:

  1. ComponentA calls Awake, enables ComponentB and ComponentC

  2. ComponentB calls Awake, disables itself

  3. 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().

avatar image Cherno · Aug 21, 2014 at 07:15 PM 0
Share

I have the same problem with one particular component, so far I'm just enabling it twice :)

avatar image b1gry4n · Aug 21, 2014 at 07:21 PM 0
Share

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

avatar image PsychoDuckArcade · Aug 21, 2014 at 10:46 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Bool Value reversed 2 Answers

How to dynamically access to the inside of the Stats value in Unity3D 0 Answers

[Improvement] change image based on set value 1 Answer

Do gameobjects pass reference by default? 1 Answer

Force uppercase in Input Field without using onValueChange? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges