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 BjarkeDuDe · Oct 16, 2012 at 08:43 PM · javascriptenablecomponents

Can a script enable components?

HEY! I just wanted to ask if there is a way to have scripts enable components on other objects. Maybe you have a timer, and when it reaches some point, it enables something like another script, or maybe a light source, or checks a mesh render on another object (or the same). That sort of thing. In javascript please ^^

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

2 Replies

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

Answer by DDelapena · Oct 16, 2012 at 09:26 PM

just get the component from the gameObject and then set ".enabled" to true or false.

on some kind of trigger event it could be like this:

 var component : nameOfComponent = collision.GetComponent( nameOfComponent );
 component.enabled = true;
 //assuming default is false

or if no collision/trigger event..find the gameObject first..and get component from that.

 var gObjectComponent : nameOfComponent;
 gObjectComponent = GameObject.Find("nameOfGameObjectWithComponent").GetComponent( nameOfComponent );
 
 if( time >= 3seconds )
      gObjectComponent.enabled = true;
 else
      gObjectComponent.enabled = false;

Comment
Add comment · Show 7 · 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 BjarkeDuDe · Oct 16, 2012 at 09:56 PM 0
Share

I get an error at the gObjectComponent = GameObject.Find("poop$$anonymous$$other).GetComponent( Light); Line: expecting "", found '\n'.

sorry for the silly names ^^

avatar image DDelapena · Oct 16, 2012 at 10:00 PM 0
Share

you're missing a " at the end of poop$$anonymous$$other in the Find function

avatar image DDelapena · Oct 16, 2012 at 10:00 PM 0
Share

GameObject.Find("poop$$anonymous$$other").GetComponent( Light);

would be correct.

avatar image DDelapena · Oct 16, 2012 at 10:01 PM 0
Share

oops, i guess i forgot that in $$anonymous$$e too. :) i'll edit it.

avatar image DDelapena · Oct 16, 2012 at 10:04 PM 0
Share

if you have multiple objects that need to set component to on, you could make a GameObject array first and run a for loop to enable all that need to be on.

For a more "pipeline efficient" setup, refer to Robs post.. he gave it to you in c# though, when you asked for js. Could easily be converted.

Show more comments
avatar image
2

Answer by Eric5h5 · Oct 16, 2012 at 08:55 PM

Just use the .enabled property (if it exists for that component). GetComponent(Blah).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 BjarkeDuDe · Oct 16, 2012 at 09:30 PM 0
Share

Thank you! Now i know how to enable it, but i forgot to mention that i don't know how to define precisely what component on what object i want. And as far as i know you can't rename components.

avatar image DDelapena · Oct 16, 2012 at 09:37 PM 0
Share

i gave a precise way to do just that.

avatar image DDelapena · Oct 16, 2012 at 09:46 PM 0
Share

I'd say it's good practice to start na$$anonymous$$g components uniquely so that you don't have multiple components of the same name on one object.

avatar image BjarkeDuDe · Oct 16, 2012 at 10:14 PM 0
Share

How do you name components?

avatar image DDelapena · Oct 16, 2012 at 10:28 PM 0
Share

what type of components are we talking about here? script components generally have the same name as the name of the script itself. I wouldn't think you can or would apply the same script component to the same object.

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

14 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

Related Questions

[SOLVED]Possible Alternation of Transform.Rotate 1 Answer

Need help with this Rotation Script (fixes) 1 Answer

Disabling the Z axis? 1 Answer

Disable a GameObjects scripts knowing only the GameObjects name 3 Answers

Unity fire1 prefab in code 2 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