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
0
Question by bustedkrutch · Feb 27, 2014 at 07:34 PM · addcomponentdisabled

Trouble with Adding a Component that is already Disabled

Hi Folks,

Really at a loss on this one.

I'm using AddComponent to add a script to a GameObject and it is added in the "enabled = false" state.

This is the code that I'm using to add the script

                     if(SC.GetComponent<SceneController_Game_Mode_CS>() == null) {
                         SC.AddComponent("SceneController_Game_Mode_CS");

I'm not sure how this happens? Can a script be saved as disabled, so that when it is added it retains that state?

If so, how can I resave it as enabled?

I really don't know what to do with this one.

Thank you for your help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by stevethorne · Feb 27, 2014 at 07:50 PM

Not sure why the issue is happening to you but you could write

 ( ( SceneController_Game_Mode_CS ) SC.AddComponent( "SceneController_Game_Mode_CS" ) ).enabled = true;
Comment
Add comment · Show 6 · Share
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 bustedkrutch · Feb 27, 2014 at 09:02 PM 0
Share

Hey Steve,

Thanks so much for the quick response, however I tried this and I got the following:

error CS1061: Type UnityEngine.Component' does not contain a definition for enabled' and no extension method enabled' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

I've also tried enabling it immediately after adding it but, that still leaves the "Awake()" routine running and causes me grief.

Thanks.

avatar image stevethorne · Feb 27, 2014 at 09:05 PM 0
Share

You'll have to cast your component to what you want to enable in order to do that.

What Awake() grief are you talking about?

avatar image bustedkrutch · Feb 27, 2014 at 09:28 PM 0
Share

hmm, can you give me a code example of the cast-ing you're suggesting as I'm not sure how to cast the "enabling" of the component, or I'm just confused :)

Awake() grief = it seems that the Awake() function runs when the script is added even though it is disabled and that is forcing me to rearrange some variable instantiations (I think this is the correct word here).

avatar image stevethorne · Feb 27, 2014 at 09:33 PM 0
Share

Awake is called no matter what, it's similar to a constructor. Even if the script is disabled, it will be called.

As for the casting I've updated my answer to fix that.

avatar image bustedkrutch · Feb 27, 2014 at 09:46 PM 0
Share

Yep, got that about Awake :)

And that casting is slick as $n0+ :) and works great!

Thanks Steve!

Show more comments

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# - Disabled Script Still Runs! 1 Answer

Football free kick question? 1 Answer

AddComponent with exposed 1 Answer

Variables assigned in the inspector of a script don't seem to work when instantiating them via AddComponent 0 Answers

How to attach a script to an instantiated object? 1 Answer


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