- Home /
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 ^^
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;
I get an error at the gObjectComponent = GameObject.Find("poop$$anonymous$$other).GetComponent( Light); Line: expecting "", found '\n'.
sorry for the silly names ^^
you're missing a " at the end of poop$$anonymous$$other in the Find function
GameObject.Find("poop$$anonymous$$other").GetComponent( Light);
would be correct.
oops, i guess i forgot that in $$anonymous$$e too. :) i'll edit it.
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.
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.
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.
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.
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.
Your answer
Follow this Question
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