- Home /
Enable script (get "script" number?)
Hello, i try to enable scripts when a object spawns, but i got 2 scripts with the same name on this object. I tried this:
ship.GetComponent<Fire>(0).enabled=true;
ship.GetComponent<Fire>(1).enabled=true;
ship.GetComponent<Raycontrol>().enabled=true;
ship.GetComponent<KeyControl>().enabled=true;
But this does not work.
How can i enable the first, and after that the second fire script?
Answer by Jeff-Kesselman · May 05, 2014 at 05:36 PM
ship.GetComponents<Fire>()[0].enabled=true;
ship.GetComponents<Fire>()[1].enabled=true;
etc
Note the plaurel GetComponents and the use of [].
BUT this is horribly, terribly inefficient. Yo usould get the array once and then access it.
Fire[] fireComponents = ship.GetComponents<Fire>();
fireComponents[0].enabled=true;
fireComponents[1].enabled=true;
etc
NOTE: If you start the game with a component not enabled, it doesn't get created as disabled, it simply doesn't get created at all.
Answer by xortrox · May 05, 2014 at 05:34 PM
To get both scripts you could use GetComponents() , as for enabling the first one after the second one, you might need to add some sort of ID to the scripts to know which is which, unless the array returned by GetComponents is in the order you need.
Your answer

Follow this Question
Related Questions
Enable a script on object in c# help 2 Answers
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
c# how to refresh my targeting list 2 Answers