Create script with script?

Hey I tried a quick google search with no luck. Everything was about how to write scripts or access things in scripts. I have a laser beam that is created during the game and I was wondering if when I create the cylinder, aka laser, if I could also create a script to use the cylinder’s capsule collider to detect a collision with an object such as a player or wall. Any other time I have used OnCollisionEnter, Exit, or Stay, the script has had to be attached to the object in this case the laser. So I was wondering if it is possible to create a script with a script or attach a script to a gameobject in a script. This way when the laser shoots I can just do one of the previously mentioned.

I know I could use an empty then attach the cylinder and then play with the alpha channel, or mesh renderer to turn the laser on and off; and if I can not create the script then this is probably what I will do, unless anyone has any better suggestions.

If you manage to get an answer through a google search, I am sorry my googlefoo is weak.

Hello,

Yes my answer is a google search, basically because this exact question has been asked so many times before :slight_smile:

but it’s quite stupid to create the objects dynamically when you could just use prefabs (sometimes this is not possible but in your case it would be the better solution):

http://lmtfy.com?q=Unity+Prefabs

Hope this helps,
Benproductions1

FYI: The reason you didn’t find any answers is probably because you searched: “Create script with script” which is also called dynamic code generation. This is not what you want. It’s always good to know the jargon XD